Skip to content

Commit

Permalink
Year 2019 Day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Sep 1, 2023
1 parent b637a73 commit e424dc0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pie
| 4 | [Secure Container](https://adventofcode.com/2019/day/4) | [Source](src/year2019/day04.rs) | 7 |
| 5 | [Sunny with a Chance of Asteroids](https://adventofcode.com/2019/day/5) | [Source](src/year2019/day05.rs) | 34 |
| 6 | [Universal Orbit Map](https://adventofcode.com/2019/day/6) | [Source](src/year2019/day06.rs) | 28 |
| 7 | [Amplification Circuit](https://adventofcode.com/2019/day/7) | [Source](src/year2019/day07.rs) | 30000 |

## 2015

Expand Down
1 change: 1 addition & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ mod year2019 {
benchmark!(year2019, day04);
benchmark!(year2019, day05);
benchmark!(year2019, day06);
benchmark!(year2019, day07);
}

mod year2015 {
Expand Down
1 change: 1 addition & 0 deletions input/year2019/day07.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,46,59,84,93,102,183,264,345,426,99999,3,9,1002,9,4,9,1001,9,3,9,102,2,9,9,1001,9,5,9,102,3,9,9,4,9,99,3,9,1002,9,3,9,101,4,9,9,4,9,99,3,9,1002,9,4,9,1001,9,4,9,102,2,9,9,1001,9,2,9,1002,9,3,9,4,9,99,3,9,1001,9,5,9,4,9,99,3,9,1002,9,4,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,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,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,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,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,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,1001,9,1,9,4,9,99,3,9,101,1,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,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub mod year2019 {
pub mod day04;
pub mod day05;
pub mod day06;
pub mod day07;
}

/// # Help Santa by solving puzzles to fix the weather machine's snow function.
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ fn all_solutions() -> Vec<Solution> {
solution!(year2019, day04),
solution!(year2019, day05),
solution!(year2019, day06),
solution!(year2019, day07),
// 2015
solution!(year2015, day01),
solution!(year2015, day02),
Expand Down
4 changes: 2 additions & 2 deletions src/year2019/day05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::util::parse::*;
use std::sync::mpsc::*;
use std::thread;

struct IntCode {
pub struct IntCode {
pc: usize,
code: Vec<i64>,
input_rx: Receiver<i64>,
Expand All @@ -13,7 +13,7 @@ struct IntCode {
impl IntCode {
const FACTOR: [i64; 4] = [0, 100, 1000, 10000];

fn spawn(code: &[i64]) -> (Sender<i64>, Receiver<i64>) {
pub fn spawn(code: &[i64]) -> (Sender<i64>, Receiver<i64>) {
let pc = 0;
let code = code.to_vec();
let (input_tx, input_rx) = channel();
Expand Down
71 changes: 71 additions & 0 deletions src/year2019/day07.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! # Amplification Circuit
//!
//! Brute force solution for both parts using the utility [`permutations`] method to test each of
//! the possible 5! or 120 permutations of the phase settings.
//!
//! Even though each `IntCode` computer runs in parallel in a separate thread the [`Sender`] and
//! [`Receiver`] objects synchronize messages between threads, blocking each thread when
//! input is needed.
//!
//! [`permutations`]: crate::util::slice
//! [`Sender`]: std::sync::mpsc::Sender
//! [`Receiver`]: std::sync::mpsc::Receiver
use super::day05::IntCode;
use crate::util::parse::*;
use crate::util::slice::*;

pub fn parse(input: &str) -> Vec<i64> {
input.iter_signed::<i64>().collect()
}

pub fn part1(input: &[i64]) -> i64 {
let mut result = 0;

let sequence = |slice: &[i64]| {
let mut signal = 0;

// Send exactly 2 inputs and receive exactly 1 output per amplifier.
for &phase in slice {
let (tx, rx) = IntCode::spawn(input);
let _ = tx.send(phase);
let _ = tx.send(signal);
signal = rx.recv().unwrap();
}

result = result.max(signal);
};

[0, 1, 2, 3, 4].permutations(sequence);
result
}

pub fn part2(input: &[i64]) -> i64 {
let mut result = 0;

let feedback = |slice: &[i64]| {
let (senders, receivers): (Vec<_>, Vec<_>) = (0..5).map(|_| IntCode::spawn(input)).unzip();

// Send each initial phase setting exactly once.
for (tx, &phase) in senders.iter().zip(slice.iter()) {
let _ = tx.send(phase);
}

// Chain amplifier inputs and ouputs in a loop until all threads finish.
let mut signal = 0;

'outer: loop {
for (tx, rx) in senders.iter().zip(receivers.iter()) {
let _ = tx.send(signal);
let Ok(next) = rx.recv() else {
break 'outer;
};
signal = next;
}
}

result = result.max(signal);
};

[5, 6, 7, 8, 9].permutations(feedback);
result
}
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod year2019 {
mod day04_test;
mod day05_test;
mod day06_test;
mod day07_test;
}

mod year2015 {
Expand Down
20 changes: 20 additions & 0 deletions tests/year2019/day07_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use aoc::year2019::day07::*;

const FIRST_EXAMPLE: &str = "\
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0";

const SECOND_EXAMPLE: &str = "\
3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,
27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5";

#[test]
fn part1_test() {
let input = parse(FIRST_EXAMPLE);
assert_eq!(part1(&input), 43210);
}

#[test]
fn part2_test() {
let input = parse(SECOND_EXAMPLE);
assert_eq!(part2(&input), 139629729);
}

0 comments on commit e424dc0

Please sign in to comment.