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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{alpha1, i64, line_ending},
combinator::map,
multi::separated_list1,
sequence::tuple,
IResult,
};
use std::{
collections::{BTreeMap, BTreeSet},
fs,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = fs::read_to_string("inputs/day_21.txt")?;
let mut monkeys = Monkeys::parser(&input).unwrap().1;
dbg!(monkeys.eval("root"));
let (original_root_left, original_root_right) =
monkeys.0.get("root").expect("No root monkey!").values();
monkeys.0.insert(
"root".into(),
Expression::Eql(original_root_left, original_root_right.expect("foo")),
);
monkeys
.0
.insert("humn".into(), Expression::Value(Value::Input));
while monkeys.0.len() > 1 {
monkeys.simplify();
}
println!("{}", monkeys.print_expression("root"));
Ok(())
}
#[derive(Debug, Clone)]
struct Monkeys(BTreeMap<String, Expression>);
#[derive(Debug, Clone)]
enum Expression {
Value(Value),
Add(Value, Value),
Sub(Value, Value),
Mul(Value, Value),
Div(Value, Value),
Eql(Value, Value),
}
#[derive(Debug, Clone)]
enum Value {
Ref(String),
Literal(i64),
Input,
}
impl Monkeys {
fn parser(input: &str) -> IResult<&str, Self> {
map(
separated_list1(line_ending, tuple((alpha1, tag(": "), Expression::parser))),
|lines| {
Monkeys(
lines
.into_iter()
.map(|(id, _, expression): (&str, _, Expression)| {
(id.to_owned(), expression)
})
.collect(),
)
},
)(input)
}
fn simplify(&mut self) {
let ids: Vec<String> = self.0.keys().cloned().collect();
// simplify to literals where possible
for id in &ids {
if let Some(val) = self.eval(id) {
self.0
.insert(id.clone(), Expression::Value(Value::Literal(val)));
}
}
// simplify single sides of expressions where possible (especially unwrap refs)
for id in &ids {
let monkey = self.0.get(id).expect("Unknown monkey!");
let simplified = match monkey {
Expression::Value(v) => Expression::Value(self.simplify_value(v)),
Expression::Add(a, b) => {
Expression::Add(self.simplify_value(a), self.simplify_value(b))
}
Expression::Sub(a, b) => {
Expression::Sub(self.simplify_value(a), self.simplify_value(b))
}
Expression::Mul(a, b) => {
Expression::Mul(self.simplify_value(a), self.simplify_value(b))
}
Expression::Div(a, b) => {
Expression::Div(self.simplify_value(a), self.simplify_value(b))
}
Expression::Eql(a, b) => {
Expression::Eql(self.simplify_value(a), self.simplify_value(b))
}
};
self.0.insert(id.clone(), simplified);
if let Some(val) = self.eval(id) {
self.0
.insert(id.clone(), Expression::Value(Value::Literal(val)));
}
}
// simplify across the equals
for id in &ids {
let monkey = self.0.get(id).expect("Unknown monkey!").clone();
match monkey {
Expression::Eql(Value::Ref(r), Value::Literal(v))
| Expression::Eql(Value::Literal(v), Value::Ref(r)) => {
let referenced_monkey = self.0.get(&r).expect("Unknown monkey!").clone();
let new_eql = match referenced_monkey {
Expression::Add(new_lhs, Value::Literal(added))
| Expression::Add(Value::Literal(added), new_lhs) => {
Some((new_lhs.clone(), v - added))
}
Expression::Sub(new_lhs, Value::Literal(subtracted)) => {
Some((new_lhs.clone(), v + subtracted))
}
Expression::Sub(Value::Literal(subtracted_from), new_lhs) => {
Some((new_lhs.clone(), subtracted_from - v))
}
Expression::Mul(new_lhs, Value::Literal(mult))
| Expression::Mul(Value::Literal(mult), new_lhs) => {
Some((new_lhs.clone(), v / mult))
}
Expression::Div(new_lhs, Value::Literal(denom)) => {
Some((new_lhs.clone(), v * denom))
}
_ => None,
};
if let Some((new_lhs, new_v)) = new_eql {
self.0
.insert(id.clone(), Expression::Eql(new_lhs, Value::Literal(new_v)));
}
}
_ => {}
}
}
// clearing out unreferenced monkeys
let monkey_references: BTreeSet<String> = self
.0
.values()
.flat_map(|expression| match expression.values() {
(Value::Ref(v1), Some(Value::Ref(v2))) => vec![v1, v2],
(Value::Ref(v), _) | (_, Some(Value::Ref(v))) => vec![v],
_ => vec![],
})
.collect();
for id in &ids {
if id != "root" && !monkey_references.contains(id) {
self.0.remove(id);
}
}
}
fn simplify_value(&self, val: &Value) -> Value {
match val {
Value::Ref(r) => {
let referenced = self.0.get(r).expect("Unknown monkey!");
match referenced {
Expression::Value(v) => v.clone(),
_ => val.clone(),
}
}
v => v.clone(),
}
}
fn eval(&self, id: &str) -> Option<i64> {
let monkey = self.0.get(id).expect("Unknown monkey!");
match monkey {
Expression::Value(v) => self.resolve_value(v),
Expression::Add(a, b) => self
.resolve_value(a)
.zip(self.resolve_value(b))
.map(|(a, b)| a + b),
Expression::Sub(a, b) => self
.resolve_value(a)
.zip(self.resolve_value(b))
.map(|(a, b)| a - b),
Expression::Mul(a, b) => self
.resolve_value(a)
.zip(self.resolve_value(b))
.map(|(a, b)| a * b),
Expression::Div(a, b) => self
.resolve_value(a)
.zip(self.resolve_value(b))
.map(|(a, b)| a / b),
Expression::Eql(a, b) => self
.resolve_value(a)
.zip(self.resolve_value(b))
.map(|(a, b)| if a == b { 1 } else { 0 }),
}
}
fn resolve_value(&self, value: &Value) -> Option<i64> {
match value {
Value::Ref(id) => self.eval(id),
Value::Literal(num) => Some(*num),
Value::Input => None,
}
}
fn print_expression(&self, id: &str) -> String {
let monkey = self.0.get(id).expect("Unknown monkey!");
// The crazy newlines here make it easier to see the
// brackets. Drop it in emacs and autoindent, and it's easy to
// see the next expression which isn't being collapsed.
match monkey {
Expression::Value(v) => format!("(\n{}\n)", self.print_value(v)),
Expression::Add(a, b) => {
format!("(\n{}\n+\n{}\n)", self.print_value(a), self.print_value(b))
}
Expression::Sub(a, b) => {
format!("(\n{}\n-\n{}\n)", self.print_value(a), self.print_value(b))
}
Expression::Mul(a, b) => {
format!("(\n{}\n*\n{}\n)", self.print_value(a), self.print_value(b))
}
Expression::Div(a, b) => {
format!("(\n{}\n/\n{}\n)", self.print_value(a), self.print_value(b))
}
Expression::Eql(a, b) => {
format!("(\n{}\n=\n{}\n)", self.print_value(a), self.print_value(b))
}
}
}
fn print_value(&self, value: &Value) -> String {
match value {
Value::Ref(id) => self.print_expression(id),
Value::Literal(num) => format!("{}", num),
Value::Input => "input".into(),
}
}
}
impl Expression {
fn parser(input: &str) -> IResult<&str, Self> {
alt((
map(
tuple((Value::parser, tag(" + "), Value::parser)),
|(a, _, b)| Expression::Add(a, b),
),
map(
tuple((Value::parser, tag(" - "), Value::parser)),
|(a, _, b)| Expression::Sub(a, b),
),
map(
tuple((Value::parser, tag(" * "), Value::parser)),
|(a, _, b)| Expression::Mul(a, b),
),
map(
tuple((Value::parser, tag(" / "), Value::parser)),
|(a, _, b)| Expression::Div(a, b),
),
map(Value::parser, Expression::Value),
))(input)
}
fn values(&self) -> (Value, Option<Value>) {
match self.clone() {
Expression::Value(v) => (v, None),
Expression::Add(a, b) => (a, Some(b)),
Expression::Sub(a, b) => (a, Some(b)),
Expression::Mul(a, b) => (a, Some(b)),
Expression::Div(a, b) => (a, Some(b)),
Expression::Eql(a, b) => (a, Some(b)),
}
}
}
impl Value {
fn parser(input: &str) -> IResult<&str, Self> {
alt((
map(i64, Value::Literal),
map(alpha1, |s: &str| Value::Ref(s.to_owned())),
))(input)
}
}
|