diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2019-12-14 20:59:58 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2019-12-14 20:59:58 +0200 |
commit | 5c60610b768b98113ca8ca5e8f833fa42d5aa4cf (patch) | |
tree | 9c4573be2e1d6a626152880e568085d62d3340aa /src/bin/day_7.rs | |
parent | 754a9a4e220b039770b12cb0803b0b0ad3133555 (diff) |
Intcode computer to use bigints and unlimited memory space!
Also started a more complete error reporting scheme.
Diffstat (limited to 'src/bin/day_7.rs')
-rw-r--r-- | src/bin/day_7.rs | 24 |
1 files changed, 13 insertions, 11 deletions
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 { |