Day 8 minor cleanup

This commit is contained in:
Daniel S. 2020-12-08 14:37:28 +01:00
parent 703d330980
commit 6d14236aa4

View file

@ -14,6 +14,18 @@ pub struct CPU {
acc: i64, 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 { impl From<&str> for Inst {
fn from(inst: &str) -> Self { fn from(inst: &str) -> Self {
let mut inst = inst.split_whitespace(); let mut inst = inst.split_whitespace();
@ -46,13 +58,7 @@ impl CPU {
} }
fn flip(&mut self, idx: usize) { fn flip(&mut self, idx: usize) {
if let Some(new_inst) = match &self.program[idx] { self.program[idx].flip();
Inst::Jmp { offset } => Some(Inst::Nop { unused: *offset }),
Inst::Nop { unused } => Some(Inst::Jmp { offset: *unused }),
_ => None,
} {
self.program[idx] = new_inst;
};
} }
fn step(&mut self) -> bool { fn step(&mut self) -> bool {