-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2019_23.rs
201 lines (165 loc) · 5.67 KB
/
solution_2019_23.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::collections::HashMap;
use std::hash::Hash;
use advent_of_code_2019::intcode::{Entry, MachineCode, Process, parse_machine_code};
use advent_of_code_common::coords2d::Coords2D;
const NAT_ADDRESS: Entry = 255;
const COMPUTER_COUNT: ComputerIndex = 50;
type XYPair = Coords2D<Entry>;
struct MessageBuffers<K: Eq + Hash, V: Clone> {
map: HashMap<K, Vec<V>>,
}
impl<K: Eq + Hash, V: Clone> MessageBuffers<K, V> {
fn new() -> MessageBuffers<K, V> {
MessageBuffers {
map: HashMap::new(),
}
}
fn push(&mut self, k: K, v: V) {
let mut messages: Vec<V> = self.map.get(&k).unwrap_or(&vec![]).clone();
messages.push(v);
self.map.insert(k, messages);
}
fn pop_messages_for(&mut self, k: &K) -> Vec<V> {
let m: &mut HashMap<K, Vec<V>> = &mut self.map;
let result = m.get(k).unwrap_or(&vec![]).clone();
m.remove(k);
result
}
}
fn machine_code() -> MachineCode {
parse_machine_code(include_str!("../../resources/23.txt"))
}
type ComputerIndex = usize;
fn create_computers() -> Vec<Process> {
(0 .. COMPUTER_COUNT)
.map(|i| {
let mut process = Process::new(&machine_code());
process.provide_input(i as Entry);
process
})
.collect()
}
fn send_message_buffers_to_computers(
computers: &mut [Process],
buffers: &mut MessageBuffers<usize, XYPair>,
) {
#[allow(clippy::needless_range_loop)]
for idx in 0 .. computers.len() {
let computer = &mut computers[idx];
let messages: Vec<XYPair> = buffers.pop_messages_for(&idx);
if messages.is_empty() {
computer.provide_input(-1 as Entry);
} else {
for message in messages {
computer.provide_input(message.x);
computer.provide_input(message.y);
}
}
}
}
fn solve_1() {
let mut computers: Vec<_> = create_computers();
let mut buffers: MessageBuffers<ComputerIndex, XYPair> = MessageBuffers::new();
loop {
// get all computers hungry for input or halted
#[allow(clippy::needless_range_loop)]
for idx in 0 .. COMPUTER_COUNT {
let computer = &mut computers[idx];
let halted = computer.run_to_unsatisfied_input();
if halted {
println!("Computer {idx} has halted");
}
}
// send the message buffers to computers
send_message_buffers_to_computers(&mut computers, &mut buffers);
// place messages from computers into message buffers
#[allow(clippy::needless_range_loop)]
#[allow(clippy::cast_sign_loss)]
for idx in 0 .. COMPUTER_COUNT {
let computer = &mut computers[idx];
if computer.output_len() >= 3 {
let addr = computer.next_output_unsafe();
let x = computer.next_output_unsafe();
let y = computer.next_output_unsafe();
println!("{addr} {x} {y}");
let xy_pair = XYPair { x, y };
if addr == NAT_ADDRESS {
println!("Part 1: {y}");
assert_eq!(y, 20_372);
return;
}
buffers.push(addr as ComputerIndex, xy_pair);
}
}
}
}
fn solve_2() {
let computer_count: ComputerIndex = 50;
let mut last_packet_received_by_nat: Option<XYPair> = None;
let mut last_y_sent_to_0: Option<Entry> = None;
let mut computers: Vec<_> = create_computers();
let mut buffers: MessageBuffers<ComputerIndex, XYPair> = MessageBuffers::new();
loop {
// get all computers hungry for input or halted
#[allow(clippy::needless_range_loop)]
for idx in 0 .. computer_count {
let computer = &mut computers[idx];
let halted = computer.run_to_unsatisfied_input();
if halted {
println!("Computer {idx} has halted");
}
}
let idle_computer_count = computers
.iter()
.filter(|c| c.unsatisfied_input() && c.output_len() == 0)
.count();
if idle_computer_count == computer_count {
if let Some(p) = last_packet_received_by_nat {
if Some(p.y) == last_y_sent_to_0 {
println!("Part 2: {}", p.y);
assert_eq!(p.y, 13_334);
return;
}
buffers.push(0, p);
last_y_sent_to_0 = Some(p.y);
}
}
// send the message buffers to computers
send_message_buffers_to_computers(&mut computers, &mut buffers);
// place messages from computers into message buffers
#[allow(clippy::needless_range_loop)]
#[allow(clippy::cast_sign_loss)]
for idx in 0 .. computer_count {
let computer = &mut computers[idx];
if computer.output_len() >= 3 {
let addr = computer.next_output_unsafe();
let x = computer.next_output_unsafe();
let y = computer.next_output_unsafe();
println!("{addr} {x} {y}");
buffers.push(addr as ComputerIndex, XYPair { x, y });
let xy_pair = XYPair { x, y };
if addr == NAT_ADDRESS {
last_packet_received_by_nat = Some(xy_pair);
} else {
buffers.push(addr as ComputerIndex, xy_pair);
}
}
}
}
}
fn main() {
solve_1();
solve_2();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_solve_1() {
solve_1();
}
#[test]
fn test_solve_2() {
solve_2();
}
}