-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24.rs
More file actions
216 lines (185 loc) · 5.39 KB
/
day24.rs
File metadata and controls
216 lines (185 loc) · 5.39 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Wire {
True,
False,
Inactive,
}
impl Wire {
pub fn from_str(wire: &str) -> Self {
match wire {
"1" => Wire::True,
"0" => Wire::False,
_ => {
unreachable!()
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Operator {
And,
Or,
Xor,
}
impl Operator {
pub fn from_str(operator: &str) -> Self {
match operator {
"AND" => Operator::And,
"OR" => Operator::Or,
"XOR" => Operator::Xor,
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Gate {
input1: String,
input2: String,
output: String,
operator: Operator,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Device {
wires: HashMap<String, Wire>,
gates: Vec<Gate>,
}
impl Device {
pub fn parse_input(input: &str) -> Self {
let mut first_part = true;
let mut wires = HashMap::new();
let mut gates = Vec::new();
for line in input.lines() {
if line.is_empty() {
first_part = false;
continue;
}
if first_part {
let (left, right) = line.trim().split_once(": ").unwrap();
wires.insert(left.to_string(), Wire::from_str(right));
} else {
let mut input1 = "".to_string();
let mut input2 = "".to_string();
let mut output = "".to_string();
let mut operator = "";
for (i, part) in line.split(" ").into_iter().enumerate() {
match i {
0 => {
input1 = part.to_string();
}
1 => {
operator = part;
}
2 => {
input2 = part.to_string();
}
3 => {}
4 => {
output = part.to_string();
}
_ => {
unreachable!()
}
}
}
if !wires.contains_key(&output) {
wires.insert(output.clone(), Wire::Inactive);
}
gates.push(Gate {
input1,
input2,
output,
operator: Operator::from_str(operator),
});
}
}
Device { wires, gates }
}
fn run_gate(&self, gate: &Gate) -> Wire {
let &input1 = self.wires.get(&gate.input1).unwrap();
let &input2 = self.wires.get(&gate.input2).unwrap();
if input1 == Wire::Inactive || input2 == Wire::Inactive {
return Wire::Inactive;
}
let test = match gate.operator {
Operator::And => input1 == Wire::True && input2 == Wire::True,
Operator::Or => input1 == Wire::True || input2 == Wire::True,
Operator::Xor => (input1 == Wire::True) ^ (input2 == Wire::True),
};
match test {
true => Wire::True,
false => Wire::False,
}
}
fn read_output(&self) -> Option<usize> {
let mut res = 0;
for (name, wire) in self.wires.iter() {
if !name.starts_with("z") || *wire == Wire::False {
continue;
}
if *wire == Wire::Inactive {
return None;
}
let shift = name.replace("z", "").parse::<usize>().unwrap();
res += 1 << shift;
}
Some(res)
}
pub fn run(&mut self) -> usize {
while !self.gates.is_empty() {
let mut new_gates = Vec::new();
for gate in self.gates.iter() {
let output = self.run_gate(&gate);
if output != Wire::Inactive {
self.wires.insert(gate.output.clone(), output);
} else {
new_gates.push(gate.clone())
}
}
self.gates = new_gates;
}
self.read_output().unwrap()
}
}
pub fn task01(input: &str) -> String {
let mut device = Device::parse_input(input);
device.run().to_string()
}
pub fn task02(_input: &str) -> String {
"Not implemented. Look at python graphviz approach".to_string()
}
#[cfg(test)]
mod tests {
use super::super::fs_utils::{read_example, read_input};
use super::*;
#[test]
fn test_task01() {
let input = read_example(24, 2);
assert_eq!(task01(&input), "2024");
}
#[test]
fn run_task01() {
let input = read_input(24);
assert_eq!(task01(&input), "59619940979346");
}
#[test]
fn test_task02() {
let input = read_example(24, 1);
assert_eq!(
task02(&input),
"Not implemented. Look at python graphviz approach"
);
}
#[test]
fn run_task02() {
let input = read_input(24);
assert_eq!(
task02(&input),
"Not implemented. Look at python graphviz approach"
);
assert_eq!(
"bpt,fkp,krj,mfm,ngr,z06,z11,z31",
"bpt,fkp,krj,mfm,ngr,z06,z11,z31"
);
}
}