-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.rs
161 lines (141 loc) · 4.33 KB
/
21.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
#![feature(test)]
use std::collections::HashMap;
#[derive(Debug)]
struct Input {
root_monkey: usize,
humn_monkey: usize,
jobs: Vec<Job>,
}
#[derive(Debug, Clone, Copy)]
enum Job {
Number(i64),
Add(usize, usize),
Sub(usize, usize),
Mul(usize, usize),
Div(usize, usize),
}
impl Job {
fn monkeys(self) -> (usize, usize) {
match self {
Job::Number(_) => panic!("not an operation monkey"),
Job::Add(a, b) => (a, b),
Job::Sub(a, b) => (a, b),
Job::Mul(a, b) => (a, b),
Job::Div(a, b) => (a, b),
}
}
}
fn setup(input: &str) -> Input {
let monkeys: HashMap<String, usize> = input
.lines()
.enumerate()
.map(|(i, line)| (line[..4].into(), i))
.collect();
let jobs = input
.lines()
.map(|line| {
let job = &line[6..];
if let Ok(num) = job.parse() {
Job::Number(num)
} else {
let mut job = job.split_whitespace();
let a = job.next().unwrap();
let op = job.next().unwrap();
let b = job.next().unwrap();
let a = *monkeys.get(a).unwrap();
let b = *monkeys.get(b).unwrap();
match op {
"+" => Job::Add(a, b),
"-" => Job::Sub(a, b),
"*" => Job::Mul(a, b),
"/" => Job::Div(a, b),
_ => panic!(),
}
}
})
.collect();
Input {
root_monkey: *monkeys.get("root").unwrap(),
humn_monkey: *monkeys.get("humn").unwrap(),
jobs,
}
}
fn yell_recursive(dp: &mut [Option<i64>], jobs: &[Job], monkey: usize) -> i64 {
if let Some(x) = dp[monkey] {
return x;
}
let mut yell = |i| yell_recursive(dp, jobs, i);
let value = match jobs[monkey] {
Job::Number(x) => x,
Job::Add(a, b) => yell(a) + yell(b),
Job::Sub(a, b) => yell(a) - yell(b),
Job::Mul(a, b) => yell(a) * yell(b),
Job::Div(a, b) => yell(a) / yell(b),
};
dp[monkey] = Some(value);
value
}
fn part1(input: &Input) -> i64 {
let mut dp = input.jobs.iter().map(|_| None).collect::<Vec<_>>();
yell_recursive(&mut dp, &input.jobs, input.root_monkey)
}
fn yell_recursive_opt(
dp: &mut [Option<Option<i64>>],
jobs: &[Job],
monkey: usize,
human: usize,
) -> Option<i64> {
if monkey == human {
return None;
}
if let Some(x) = dp[monkey] {
return x;
}
let mut yell = |i| yell_recursive_opt(dp, jobs, i, human);
let value = match jobs[monkey] {
Job::Number(x) => Some(x),
job => {
let (a, b) = job.monkeys();
let (Some(a), Some(b)) = (yell(a), yell(b)) else {
return None;
};
match job {
Job::Add(_, _) => Some(a + b),
Job::Sub(_, _) => Some(a - b),
Job::Mul(_, _) => Some(a * b),
Job::Div(_, _) => Some(a / b),
_ => unreachable!(),
}
}
};
dp[monkey] = Some(value);
value
}
fn part2(input: &Input) -> i64 {
let mut dp = input.jobs.iter().map(|_| None).collect::<Vec<_>>();
let mut yell = |i| yell_recursive_opt(&mut dp, &input.jobs, i, input.humn_monkey);
let (a, b) = input.jobs[input.root_monkey].monkeys();
let (x, y) = (yell(a), yell(b));
let (mut num, mut root) = match (x, y) {
(None, Some(y)) => (y, a),
(Some(x), None) => (x, b),
_ => panic!(),
};
while root != input.humn_monkey {
let (a, b) = input.jobs[root].monkeys();
let (x, y) = (yell(a), yell(b));
(num, root) = match (input.jobs[root], x, y) {
(Job::Add(_, _), None, Some(y)) => (num - y, a),
(Job::Add(_, _), Some(x), None) => (num - x, b),
(Job::Sub(_, _), None, Some(y)) => (num + y, a),
(Job::Sub(_, _), Some(x), None) => (x - num, b),
(Job::Mul(_, _), None, Some(y)) => (num / y, a),
(Job::Mul(_, _), Some(x), None) => (num / x, b),
(Job::Div(_, _), None, Some(y)) => (num * y, a),
(Job::Div(_, _), Some(x), None) => (x / num, b),
_ => panic!(),
};
}
num
}
aoc::main!(2022, 21, ex: 1);