From 39914ab0ccaed8171a857a04af9d1c2eeeda8e26 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 12 Dec 2016 13:13:20 +0200 Subject: Better regexes --- aoc12/src/main.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'aoc12') diff --git a/aoc12/src/main.rs b/aoc12/src/main.rs index e3b6038..2356402 100644 --- a/aoc12/src/main.rs +++ b/aoc12/src/main.rs @@ -24,30 +24,37 @@ impl Instruction { let jnz_lit = Regex::new(r"jnz ([-\d]+) ([-\d]+)").unwrap(); let jnz_reg = Regex::new(r"jnz (a|b|c|d) ([-\d]+)").unwrap(); - if cpy_lit.is_match(line) { - let cap = cpy_lit.captures(line).unwrap(); + let cpy_lit_match = cpy_lit.captures(line); + let cpy_reg_match = cpy_reg.captures(line); + let inc_match = inc.captures(line); + let dec_match = dec.captures(line); + let jnz_lit_match = jnz_lit.captures(line); + let jnz_reg_match = jnz_reg.captures(line); + + if cpy_lit_match.is_some() { + let cap = cpy_lit_match.unwrap(); let src: i32 = cap.at(1).unwrap().parse().unwrap(); let dest = to_register_index(cap.at(2).unwrap()); Instruction::CpyLit(src, dest) } - else if cpy_reg.is_match(line) { - let cap = cpy_reg.captures(line).unwrap(); + else if cpy_reg_match.is_some() { + let cap = cpy_reg_match.unwrap(); let src = to_register_index(cap.at(1).unwrap()); let dest = to_register_index(cap.at(2).unwrap()); Instruction::CpyReg(src, dest) } - else if inc.is_match(line) { - let cap = inc.captures(line).unwrap(); + else if inc_match.is_some() { + let cap = inc_match.unwrap(); let dest = to_register_index(cap.at(1).unwrap()); Instruction::Inc(dest) } - else if dec.is_match(line) { - let cap = dec.captures(line).unwrap(); + else if dec_match.is_some() { + let cap = dec_match.unwrap(); let dest = to_register_index(cap.at(1).unwrap()); Instruction::Dec(dest) } - else if jnz_lit.is_match(line) { - let cap = jnz_lit.captures(line).unwrap(); + else if jnz_lit_match.is_some() { + let cap = jnz_lit_match.unwrap(); let condition: i32 = cap.at(1).unwrap().parse().unwrap(); let offset: i32 = cap.at(2).unwrap().parse().unwrap(); if condition != 0 { @@ -57,8 +64,8 @@ impl Instruction { Instruction::Noop } } - else if jnz_reg.is_match(line) { - let cap = jnz_reg.captures(line).unwrap(); + else if jnz_reg_match.is_some() { + let cap = jnz_reg_match.unwrap(); let condition = to_register_index(cap.at(1).unwrap()); let offset: i32 = cap.at(2).unwrap().parse().unwrap(); Instruction::Jnz(condition, offset) -- cgit v1.2.3