aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar alecdwm 2019-12-08 00:20:28 +1000
committerGravatar alecdwm 2019-12-08 00:21:49 +1000
commit81edc123cb485ee102ad23ed5fa7bee7226810fd (patch)
tree3c7ff2709ac890a2f531d25cfe0982c3c8ad9ddc
parent4a0c704bedd5fc573f7a5c22fa80d5750c4a295f (diff)
added 2019 day7 part1 solution
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs1
-rw-r--r--src/year_2019/day7.rs126
3 files changed, 128 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 4f8848e..d3f3197 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -35,4 +35,5 @@ pub mod year_2019 {
pub mod day4;
pub mod day5;
pub mod day6;
+ pub mod day7;
}
diff --git a/src/main.rs b/src/main.rs
index 0b01276..3969ff9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -31,6 +31,7 @@ fn main() {
puzzle_solutions.insert("2019::day5::part2", advent_of_code::year_2019::day5::part2);
puzzle_solutions.insert("2019::day6::part1", advent_of_code::year_2019::day6::part1);
puzzle_solutions.insert("2019::day6::part2", advent_of_code::year_2019::day6::part2);
+ puzzle_solutions.insert("2019::day7::part1", advent_of_code::year_2019::day7::part1);
let command = match env::args().nth(1) {
Some(command) => command,
diff --git a/src/year_2019/day7.rs b/src/year_2019/day7.rs
new file mode 100644
index 0000000..d772ebc
--- /dev/null
+++ b/src/year_2019/day7.rs
@@ -0,0 +1,126 @@
+//! --- Day 7: Amplification Circuit ---
+
+use super::{IntcodeComputer, IntcodeProgram};
+use itertools::Itertools;
+
+/// Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed on the ship.
+///
+/// There are five amplifiers connected in series; each one receives an input signal and produces an output signal. They are connected such that the first amplifier's output leads to the second amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. The first amplifier's input value is 0, and the last amplifier's output leads to your ship's thrusters.
+///
+/// O-------O O-------O O-------O O-------O O-------O
+/// 0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters)
+/// O-------O O-------O O-------O O-------O O-------O
+///
+/// The Elves have sent you some Amplifier Controller Software (your puzzle input), a program that should run on your existing Intcode computer. Each amplifier will need to run a copy of the program.
+///
+/// When a copy of the program starts running on an amplifier, it will first use an input instruction to ask the amplifier for its current phase setting (an integer from 0 to 4). Each phase setting is used exactly once, but the Elves can't remember which amplifier needs which phase setting.
+///
+/// The program will then call another input instruction to get the amplifier's input signal, compute the correct output signal, and supply it back to the amplifier with an output instruction. (If the amplifier has not yet received an input signal, it waits until one arrives.)
+///
+/// Your job is to find the largest output signal that can be sent to the thrusters by trying every possible combination of phase settings on the amplifiers. Make sure that memory is not shared or reused between copies of the program.
+///
+/// For example, suppose you want to try the phase setting sequence 3,1,2,4,0, which would mean setting amplifier A to phase setting 3, amplifier B to setting 1, C to 2, D to 4, and E to 0. Then, you could determine the output signal that gets sent from amplifier E to the thrusters with the following steps:
+///
+/// Start the copy of the amplifier controller software that will run on amplifier A. At its first input instruction, provide it the amplifier's phase setting, 3. At its second input instruction, provide it the input signal, 0. After some calculations, it will use an output instruction to indicate the amplifier's output signal.
+/// Start the software for amplifier B. Provide it the phase setting (1) and then whatever output signal was produced from amplifier A. It will then produce a new output signal destined for amplifier C.
+/// Start the software for amplifier C, provide the phase setting (2) and the value from amplifier B, then collect its output signal.
+/// Run amplifier D's software, provide the phase setting (4) and input value, and collect its output signal.
+/// Run amplifier E's software, provide the phase setting (0) and input value, and collect its output signal.
+///
+/// The final output signal from amplifier E would be sent to the thrusters. However, this phase setting sequence may not have been the best one; another sequence might have sent a higher signal to the thrusters.
+///
+/// Here are some example programs:
+///
+/// Max thruster signal 43210 (from phase setting sequence 4,3,2,1,0):
+///
+/// 3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
+///
+/// Max thruster signal 54321 (from phase setting sequence 0,1,2,3,4):
+///
+/// 3,23,3,24,1002,24,10,24,1002,23,-1,23,
+/// 101,5,23,23,1,24,23,23,4,23,99,0,0
+///
+/// Max thruster signal 65210 (from phase setting sequence 1,0,4,3,2):
+///
+/// 3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
+/// 1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0
+///
+/// Try every combination of phase settings on the amplifiers. What is the highest signal that can be sent to the thrusters?
+pub fn part1() {
+ let input = crate::common::read_stdin_to_string();
+ let amplifier_controller = IntcodeProgram::from(input.as_str());
+ let highest_signal = calculate_highest_signal(&amplifier_controller);
+
+ println!(
+ "The highest signal that can be sent to the thrusters: {}",
+ highest_signal
+ );
+}
+
+fn calculate_highest_signal(amplifier_controller: &IntcodeProgram) -> i64 {
+ const PHASE_SETTINGS_START: i64 = 0;
+ const PHASE_SETTINGS_COUNT: usize = 5;
+
+ (PHASE_SETTINGS_START..PHASE_SETTINGS_COUNT as i64)
+ .permutations(PHASE_SETTINGS_COUNT)
+ .map(|phase_settings| {
+ phase_settings
+ .iter()
+ .map(|phase_setting| Amplifier::new(amplifier_controller, *phase_setting))
+ .fold(0, |signal, amplifier| amplifier.amplify_signal(signal))
+ })
+ .max()
+ .expect("Failed to generate any phase settings")
+}
+
+struct Amplifier<'a> {
+ controller_rom: &'a IntcodeProgram,
+ phase_setting: i64,
+}
+
+impl<'a> Amplifier<'a> {
+ fn new(controller_rom: &'a IntcodeProgram, phase_setting: i64) -> Self {
+ Self {
+ controller_rom,
+ phase_setting,
+ }
+ }
+
+ fn amplify_signal(&self, signal: i64) -> i64 {
+ let mut controller = IntcodeComputer::from(self.controller_rom);
+ let input = controller.create_input();
+ let output = controller.create_output();
+
+ input
+ .send(self.phase_setting)
+ .expect("Failed to send phase_setting to amplifier_controller");
+ input
+ .send(signal)
+ .expect("Failed to send signal to amplifier_controller");
+
+ controller.run();
+
+ output
+ .recv()
+ .expect("Failed to receive amplifier_controller output")
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_part1_examples() {
+ let examples = [
+ ("3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0", 43210),
+ ("3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0", 54321),
+ ("3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0", 65210)
+ ];
+
+ for example in &examples {
+ let amplifier_controller = IntcodeProgram::from(example.0);
+ assert_eq!(calculate_highest_signal(&amplifier_controller), example.1);
+ }
+ }
+}