Day 8 minor cleanup

This commit is contained in:
Daniel S. 2020-12-08 14:37:28 +01:00
parent 703d330980
commit 6d14236aa4
1 changed files with 13 additions and 7 deletions

View File

@ -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 {