Day 8 minor cleanup
This commit is contained in:
parent
703d330980
commit
6d14236aa4
1 changed files with 13 additions and 7 deletions
20
src/day8.rs
20
src/day8.rs
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue