Skip to content

Commit

Permalink
2019: Day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvw committed Apr 3, 2024
1 parent f7e1294 commit f4d0675
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2019/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ $(outputdir)/puzzle_%: $(srcbindir)/puzzle_%$(ext)

$(outputdir)/puzzle_02: $(intcode_deps)
$(outputdir)/puzzle_05: $(intcode_deps)
$(outputdir)/puzzle_07: $(intcode_deps)

.PHONY: lint
lint: lint-code lint-format
Expand Down
1 change: 1 addition & 0 deletions 2019/input_07.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,34,47,72,93,110,191,272,353,434,99999,3,9,102,3,9,9,1001,9,3,9,4,9,99,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,101,3,9,9,1002,9,3,9,1001,9,2,9,1002,9,2,9,101,4,9,9,4,9,99,3,9,1002,9,3,9,101,5,9,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,101,3,9,9,102,4,9,9,1001,9,3,9,4,9,99,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99
44 changes: 44 additions & 0 deletions 2019/src/bin/puzzle_07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::io;

use aoc2019::intcode;

fn run_amplifier_loop(program: &[i32], phase_settings: &[i32]) -> i32 {
let mut amplifiers: Vec<_> = phase_settings
.iter()
.map(|&phase_setting| intcode::Computer::new(program, &[phase_setting]))
.collect();

let mut signal = 0;

'feedback_loop: loop {
for a in &mut amplifiers {
a.input.push_back(signal);
signal = match a.run() {
intcode::State::Output(x) => x,
intcode::State::Halt => break 'feedback_loop,
};
}
}

signal
}

fn main() {
let amplifier_controller_program = intcode::parse_program(io::stdin());

println!(
"Part 1: {}",
aoc2019::iter::permutation(&[0, 1, 2, 3, 4])
.map(|x| run_amplifier_loop(&amplifier_controller_program, &x))
.max()
.unwrap()
);

println!(
"Part 2: {}",
aoc2019::iter::permutation(&[5, 6, 7, 8, 9])
.map(|x| run_amplifier_loop(&amplifier_controller_program, &x))
.max()
.unwrap()
);
}

0 comments on commit f4d0675

Please sign in to comment.