diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/day_2.rs | 17 | ||||
-rw-r--r-- | src/bin/day_7.rs | 24 |
2 files changed, 23 insertions, 18 deletions
diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs index ce8fcaa..ba9e189 100644 --- a/src/bin/day_2.rs +++ b/src/bin/day_2.rs @@ -41,7 +41,9 @@ fn main() { match (opt.noun, opt.verb, opt.output) { (Some(noun), Some(verb), _) => { let result = exit_on_failed_assertion( - program.with_noun_verb_input(noun, verb).execute(), + program + .with_noun_verb_input(noun, verb) + .execute_returning_memory_0(), "Program errored", ); println!("{}", result); @@ -52,7 +54,8 @@ fn main() { println!("({}, {})", noun, verb); } (None, None, None) => { - let result = exit_on_failed_assertion(program.execute(), "Program errored"); + let result = + exit_on_failed_assertion(program.execute_returning_memory_0(), "Program errored"); println!("{}", result); } _ => { @@ -77,17 +80,17 @@ fn find_input( output: Intcode, ) -> Result<(Intcode, Intcode), IntcodeProgramError> { (0..99) - .flat_map(|noun| (0..99).map(move |verb| (noun, verb))) + .flat_map(|noun| (0..99).map(move |verb| (Intcode::from(noun), Intcode::from(verb)))) .map(|(noun, verb)| { ( - noun, - verb, + noun.clone(), + verb.clone(), program .with_noun_verb_input(noun, verb) .execute_returning_memory_0(), ) }) - .find(|(_noun, _verb, out)| *out == Ok(Some(output))) + .find(|(_noun, _verb, out)| *out == Ok(output.clone())) .map(|(noun, verb, _out)| Ok((noun, verb))) - .unwrap_or(Err(IntcodeProgramError)) + .unwrap_or(Err(IntcodeProgramError::Unknown)) } diff --git a/src/bin/day_7.rs b/src/bin/day_7.rs index 0abf215..9b9177a 100644 --- a/src/bin/day_7.rs +++ b/src/bin/day_7.rs @@ -52,12 +52,12 @@ fn find_max_power( feedback_loop_mode: bool, ) -> Result<Intcode, IntcodeProgramError> { PhaseSetting::all(feedback_loop_mode) - .map(|phase| AmplifierArray::new(program, phase).execute()) + .map(|phase| AmplifierArray::new(program, &phase).execute()) .collect::<Result<Vec<Intcode>, _>>() - .map(|powers| powers.into_iter().max().unwrap_or(0)) + .map(|powers| powers.into_iter().max().unwrap_or(Intcode::from(0))) } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] struct PhaseSetting([Intcode; 5]); impl PhaseSetting { @@ -69,7 +69,7 @@ impl PhaseSetting { } } - fn permute(min: Intcode, max: Intcode) -> impl Iterator<Item = PhaseSetting> { + fn permute(min: i32, max: i32) -> impl Iterator<Item = PhaseSetting> { // This is an absolutely atrocious way to do the permutation, // but luckily it's only 5 elements long. (min..max) @@ -77,12 +77,14 @@ impl PhaseSetting { (min..max).flat_map(move |b| { (min..max).flat_map(move |c| { (min..max).flat_map(move |d| { - (min..max).map(move |e| PhaseSetting([a, b, c, d, e])) + (min..max).map(move |e| { + PhaseSetting([a.into(), b.into(), c.into(), d.into(), e.into()]) + }) }) }) }) }) - .filter(move |phase| (min..max).all(|x| phase.0.contains(&x))) + .filter(move |phase| (min..max).all(|x| phase.0.contains(&x.into()))) } } @@ -92,7 +94,7 @@ struct AmplifierArray { } impl AmplifierArray { - fn new(program: &IntcodeProgram, phase: PhaseSetting) -> AmplifierArray { + fn new(program: &IntcodeProgram, phase: &PhaseSetting) -> AmplifierArray { AmplifierArray { amplifiers: (0..5) .map(|n| AmplifierArray::new_amp(program, phase, n)) @@ -100,11 +102,11 @@ impl AmplifierArray { } } - fn new_amp(program: &IntcodeProgram, phase: PhaseSetting, n: usize) -> IntcodeProgram { + fn new_amp(program: &IntcodeProgram, phase: &PhaseSetting, n: usize) -> IntcodeProgram { if n == 0 { - program.with_input(list![phase.0[n], 0]) + program.with_input(list![phase.0[n].clone(), Intcode::from(0)]) } else { - program.with_input(list![phase.0[n]]) + program.with_input(list![phase.0[n].clone()]) } } @@ -122,7 +124,7 @@ impl AmplifierArray { self.amplifiers .first() .and_then(|amp| amp.input.first().cloned()) - .ok_or(IntcodeProgramError) + .ok_or(IntcodeProgramError::Unknown) } fn is_terminated(&self) -> bool { |