aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-03 01:38:13 +1000
committerGravatar alecdwm 2019-12-03 01:38:13 +1000
commit083696a04fa5fe1181e6244d8120abfead81469e (patch)
treec913c5689bdde359b950781d13e8f22cc8abf728
parentd78a6be318fbb37a8883179893ec3441cc1a0ab5 (diff)
refactored 2019 day2 part2 solution
-rw-r--r--src/year_2019/day2.rs48
1 files changed, 23 insertions, 25 deletions
diff --git a/src/year_2019/day2.rs b/src/year_2019/day2.rs
index 039cd9a..2b6df51 100644
--- a/src/year_2019/day2.rs
+++ b/src/year_2019/day2.rs
@@ -89,37 +89,35 @@ pub fn part2() {
const DESIRED_OUTPUT: usize = 19690720;
- let mut noun = 0;
- let mut verb = 0;
+ let mut result = None;
+ 'outer: for noun in 0..100 {
+ for verb in 0..100 {
+ computer.memory.replace(1, noun);
+ computer.memory.replace(2, verb);
- loop {
- computer.memory.replace(1, noun);
- computer.memory.replace(2, verb);
+ computer = computer.run();
- computer = computer.run();
-
- if *computer.memory.get(0) == DESIRED_OUTPUT {
- break;
- }
-
- verb = (verb + 1) % 100;
- if verb == 0 {
- noun += 1;
-
- if noun > 99 {
- panic!(
- "No combination of verb and noun resulted in {}!",
- DESIRED_OUTPUT
- );
+ if *computer.memory.get(0) == DESIRED_OUTPUT {
+ result = Some((noun, verb));
+ break 'outer;
}
- }
- computer.load(&program);
+ computer.load(&program);
+ }
}
- let result = 100 * noun + verb;
-
- println!("100 * noun ({}) + verb ({}): {}", noun, verb, result);
+ match result {
+ None => panic!(
+ "No combination of noun and verb resulted in {}!",
+ DESIRED_OUTPUT
+ ),
+ Some((noun, verb)) => println!(
+ "100 * noun ({}) + verb ({}): {}",
+ noun,
+ verb,
+ 100 * noun + verb
+ ),
+ }
}
#[derive(Debug)]