diff --git a/src/day8.rs b/src/day8.rs index 4b6883a..bdedf96 100644 --- a/src/day8.rs +++ b/src/day8.rs @@ -14,6 +14,18 @@ pub struct CPU { acc: i64, } +impl Inst { + fn flip(&mut self) { + if let Some(new_inst) = match &self { + Inst::Jmp { offset } => Some(Inst::Nop { unused: *offset }), + Inst::Nop { unused } => Some(Inst::Jmp { offset: *unused }), + _ => None, + } { + *self=new_inst; + }; + } +} + impl From<&str> for Inst { fn from(inst: &str) -> Self { let mut inst = inst.split_whitespace(); @@ -46,13 +58,7 @@ impl CPU { } fn flip(&mut self, idx: usize) { - if let Some(new_inst) = match &self.program[idx] { - Inst::Jmp { offset } => Some(Inst::Nop { unused: *offset }), - Inst::Nop { unused } => Some(Inst::Jmp { offset: *unused }), - _ => None, - } { - self.program[idx] = new_inst; - }; + self.program[idx].flip(); } fn step(&mut self) -> bool {