aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-08 00:36:30 +1000
committerGravatar alecdwm 2019-12-08 00:36:30 +1000
commitaa1a51e5c5a64f07167dd17cad58ce6f668bab3c (patch)
tree58c3695a56d6a173434a4cbf4181012e374f9607
parent81edc123cb485ee102ad23ed5fa7bee7226810fd (diff)
refactored IntcodeComputer to not consume self on run()
-rw-r--r--src/year_2019/day2.rs15
-rw-r--r--src/year_2019/intcode_computer.rs6
2 files changed, 9 insertions, 12 deletions
diff --git a/src/year_2019/day2.rs b/src/year_2019/day2.rs
index ca8621a..0405b5a 100644
--- a/src/year_2019/day2.rs
+++ b/src/year_2019/day2.rs
@@ -59,11 +59,11 @@ pub fn part1() {
computer.memory.replace(1, 12);
computer.memory.replace(2, 2);
- let result = computer.run();
+ computer.run();
println!(
"The value left at position 0 after the program halts: {}",
- result.memory.get(0)
+ computer.memory.get(0)
);
}
@@ -97,7 +97,7 @@ pub fn part2() {
computer.memory.replace(1, noun);
computer.memory.replace(2, verb);
- computer = computer.run();
+ computer.run();
if computer.memory.get(0) == DESIRED_OUTPUT {
result = Some((noun, verb));
@@ -136,12 +136,11 @@ mod tests {
];
for example in &examples {
- let result = IntcodeComputer::from(example.0)
- .run()
- .memory
- .data_serialized();
+ let mut computer = IntcodeComputer::from(example.0);
- assert_eq!(result, example.1);
+ computer.run();
+
+ assert_eq!(computer.memory.data_serialized(), example.1);
}
}
}
diff --git a/src/year_2019/intcode_computer.rs b/src/year_2019/intcode_computer.rs
index e2cc9e9..fea5ef9 100644
--- a/src/year_2019/intcode_computer.rs
+++ b/src/year_2019/intcode_computer.rs
@@ -27,9 +27,9 @@ impl IntcodeComputer {
output_rx
}
- pub fn run(mut self) -> Self {
+ pub fn run(&mut self) {
loop {
- let next_instruction = IntcodeInstruction::from(&self);
+ let next_instruction = IntcodeInstruction::from(&*self);
let instruction_pointer_before_instruction = self.instruction_pointer;
let instruction_length = next_instruction.length();
@@ -126,8 +126,6 @@ impl IntcodeComputer {
self.instruction_pointer += instruction_length;
}
}
-
- self
}
}