summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2019-12-15 23:50:51 +0200
committerJustin Wernick <justin@worthe-it.co.za>2019-12-15 23:50:51 +0200
commitb8b6c411125f1f12cb4bbcde997141559b3ea797 (patch)
tree8f9f9140821f96b1d1d1fc13d9f416c828a8a692 /src/lib.rs
parentf6e829afa6a19225d505273eaaeee30ea4a4860b (diff)
Debuggability and fixing the day 9 bug
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs42
1 files changed, 32 insertions, 10 deletions
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<Intcode> 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::<Vec<_>>()),
+ format!("{:?}", self.input.iter().map(|n| format!("{}", n)).collect::<Vec<_>>()),
+ format!("{:?}", self.output.iter().map(|n| format!("{}", n)).collect::<Vec<_>>()),
+ )
+ }
+}
+
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<i32>) -> Vector<Intcode> {
+ input.into_iter().map(Intcode::from).collect()
+ }
+
fn test_example_program(
before_execution: Vec<i32>,
after_execution: Vec<i32>,
@@ -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));
}
}