-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07.rs
More file actions
48 lines (44 loc) · 2.33 KB
/
Copy path07.rs
File metadata and controls
48 lines (44 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::time::Instant;
use itertools::Itertools;
use intcoder::{IntCoder, ExitCode};
const PROGRAM: [i64; 523] = [3,8,1001,8,10,8,105,1,0,0,21,42,67,84,97,118,199,280,361,442,99999,3,9,101,4,9,9,102,5,9,9,101,2,9,9,1002,9,2,9,4,9,99,3,9,101,5,9,9,102,5,9,9,1001,9,5,9,102,3,9,9,1001,9,2,9,4,9,99,3,9,1001,9,5,9,1002,9,2,9,1001,9,5,9,4,9,99,3,9,1001,9,5,9,1002,9,3,9,4,9,99,3,9,102,4,9,9,101,4,9,9,102,2,9,9,101,3,9,9,4,9,99,3,9,102,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,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,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,99,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,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,101,1,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,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,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,102,2,9,9,4,9,3,9,102,2,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,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,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,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,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,1001,9,2,9,4,9,99];
fn part_one() -> i64 {
[0,1,2,3,4].iter()
.permutations(5)
.map(|digits| digits.iter()
.fold(0, |prev, &&i| {
let mut cpu = IntCoder::new(&PROGRAM);
cpu.push_input(i);
cpu.push_input(prev);
cpu.execute_until_output()
})
)
.max()
.unwrap()
}
fn part_two() -> i64 {
[5,6,7,8,9].iter()
.permutations(5)
.map(|digits| {
let mut cpus = vec![IntCoder::new(&PROGRAM); 5];
for i in 0..5 { cpus[i].push_input(*digits[i]); }
let mut input = 0;
for i in (0..5).cycle() {
cpus[i].push_input(input);
match cpus[i].execute() {
ExitCode::Output(o) => input = o,
ExitCode::Halted => return input,
ExitCode::AwaitInput => unreachable!(),
}
}
unreachable!()
})
.max()
.unwrap()
}
fn main() {
let now = Instant::now();
println!("Part one: {}", part_one());
println!("Part two: {}", part_two());
println!("Time: {}ms", now.elapsed().as_millis());
}