-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
85 lines (79 loc) · 2.4 KB
/
main.rs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use itertools::Itertools;
use std::fs;
use std::sync::mpsc;
use std::thread;
mod computer;
use computer::Computer;
fn get_input(fp: &str) -> Vec<isize> {
fs::read_to_string(fp)
.expect("Cannot read file")
.split(',')
.map(|x| x.trim().parse().unwrap())
.collect()
}
fn part_one(program: &[isize]) -> isize {
let mut max = 0;
for seq in (0..5).permutations(5) {
let mut inp = 0;
let mut input_senders = Vec::new();
let mut output_receivers = Vec::new();
for i in 0..5 {
let (input_sender, input_receiver) = mpsc::channel();
input_senders.push(input_sender);
let (output_sender, output_receiver) = mpsc::channel();
output_receivers.push(output_receiver);
let mut c = Computer::new(program.to_vec(), input_receiver, output_sender);
thread::spawn(move || {
c.run();
});
input_senders[i].send(seq[i]).unwrap();
input_senders[i].send(inp).unwrap();
inp = output_receivers[i].recv().unwrap();
}
if inp > max {
max = inp;
}
}
max
}
fn part_two(program: &[isize]) -> isize {
let mut max = 0;
for seq in (5..10).permutations(5) {
let mut input_senders = Vec::new();
let mut output_receivers = Vec::new();
for i in 0..5 {
let (input_sender, input_receiver) = mpsc::channel();
input_senders.push(input_sender);
let (output_sender, output_receiver) = mpsc::channel();
output_receivers.push(output_receiver);
let mut c = Computer::new(program.to_vec(), input_receiver, output_sender);
thread::spawn(move || {
c.run();
});
input_senders[i].send(seq[i]).unwrap();
}
let mut inp = 0;
let mut i = 0;
loop {
let j = i%5;
if input_senders[j].send(inp).is_err() {
break;
}
if let Ok(x) = output_receivers[j].recv() {
inp = x;
} else {
break;
}
i += 1;
}
if inp > max {
max = inp;
}
}
max
}
fn main() {
let program = get_input("../input.txt");
println!("Part one: {}", part_one(&program));
println!("Part two: {}", part_two(&program));
}