From b8b6c411125f1f12cb4bbcde997141559b3ea797 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sun, 15 Dec 2019 23:50:51 +0200 Subject: Debuggability and fixing the day 9 bug --- Cargo.toml | 5 ++++- src/lib.rs | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fc97025..f3ec006 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,7 @@ derive_more = "0.99.2" im = "14.0.0" rpds = "0.7.0" archery = "0.3.0" -num = "0.2" \ No newline at end of file +num = "0.2" + +[profile.release] +debug = true diff --git a/src/lib.rs b/src/lib.rs index 57d0cb1..39966e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ use std::iter::IntoIterator; pub type Intcode = BigInt; -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct IntcodeProgram { instruction_pointer: Intcode, relative_base: Intcode, @@ -40,6 +40,23 @@ impl FromIterator for IntcodeProgram { } } +impl fmt::Debug for IntcodeProgram { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!( + f, + "IntcodeProgram {{ \nins: {}, base: {}, error: {:?}, halted: {}, awaiting_input: {} \nmemory: {}, \ninput: {}, \n output: {} \n}}", + self.instruction_pointer, + self.relative_base, + self.error, + self.halted, + self.awaiting_input, + format!("{:?}", self.memory.iter().map(|(k, v)| format!("{} -> {}", k, v)).collect::>()), + format!("{:?}", self.input.iter().map(|n| format!("{}", n)).collect::>()), + format!("{:?}", self.output.iter().map(|n| format!("{}", n)).collect::>()), + ) + } +} + impl IntcodeProgram { pub fn with_noun_verb_input(&self, noun: Intcode, verb: Intcode) -> IntcodeProgram { self.with_memory_set(1.into(), noun) @@ -154,6 +171,7 @@ impl IntcodeProgram { } } fn next(&self) -> IntcodeProgram { + //dbg!(self); self.memory .get(&self.instruction_pointer) .map(|opcode| match opcode.to_radix_le(100).1[0] { @@ -261,7 +279,9 @@ impl IntcodeProgram { fn set_relative_base(&self, mode: &Intcode) -> IntcodeProgram { match self.get(1, mode) { - base_change => self.with_relative_base(self.relative_base.clone() + base_change), + base_change => self + .with_instruction_pointer_offset(2) + .with_relative_base(self.relative_base.clone() + base_change), } } @@ -384,6 +404,10 @@ mod tests { .collect() } + fn i32_vec_to_intcode_vec(input: Vec) -> Vector { + input.into_iter().map(Intcode::from).collect() + } + fn test_example_program( before_execution: Vec, after_execution: Vec, @@ -412,13 +436,11 @@ mod tests { #[test] fn day_9_example_1() { - let program = test_example_program( - vec![ - 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99, - ], - vec![ - 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99, - ], - ); + let quine = vec![ + 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99, + ]; + let program = i32_vec_to_intcode_program(quine.clone()).run_to_termination(); + assert_eq!(program.error, None); + assert_eq!(program.output, i32_vec_to_intcode_vec(quine)); } } -- cgit v1.2.3