-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b637a73
commit e424dc0
Showing
9 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ mod year2019 { | |
mod day04_test; | ||
mod day05_test; | ||
mod day06_test; | ||
mod day07_test; | ||
} | ||
|
||
mod year2015 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |