diff options
-rw-r--r-- | src/bin/day_2.rs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs index f1a1ac7..172a89a 100644 --- a/src/bin/day_2.rs +++ b/src/bin/day_2.rs @@ -115,10 +115,14 @@ impl Program { } fn execute(&self) -> Result<Intcode, ProgramError> { - if self.run_to_termination().error { + self.run_to_termination().into_result() + } + + fn into_result(&self) -> Result<Intcode, ProgramError> { + if self.error { Err(ProgramError) } else { - Ok(self.run_to_termination().codes.head().unwrap().clone()) + Ok(self.codes.head().unwrap().clone()) } } @@ -140,10 +144,10 @@ impl Program { } fn add(&self) -> Program { - match (self.get_deref(1), self.get_dered(2), self.get(3)) { + match (self.get_deref(1), self.get_deref(2), self.get(3)) { (Some(in1), Some(in2), Some(out)) => Program { program_counter: self.program_counter + 4, - codes: self.codes.update(*out as usize, in1 + in2), + codes: self.codes.update(out as usize, in1 + in2), ..self.clone() }, _ => self.error(), @@ -151,10 +155,10 @@ impl Program { } fn multiply(&self) -> Program { - match (self.get_deref(1), self.get_dered(2), self.get(3)) { + match (self.get_deref(1), self.get_deref(2), self.get(3)) { (Some(in1), Some(in2), Some(out)) => Program { program_counter: self.program_counter + 4, - codes: self.codes.update(*out as usize, in1 * in2), + codes: self.codes.update(out as usize, in1 * in2), ..self.clone() }, _ => self.error(), @@ -177,12 +181,15 @@ impl Program { } fn get(&self, pointer_offset: usize) -> Option<Intcode> { - self.codes.get(self.program_counter + pointer_offset) + self.codes + .get(self.program_counter + pointer_offset) + .cloned() } fn get_deref(&self, pointer_offset: usize) -> Option<Intcode> { self.get(pointer_offset) - .and_then(|r| self.codes.get(*r as usize)) + .and_then(|r| self.codes.get(r as usize)) + .cloned() } } |