-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
222 lines (205 loc) · 6.17 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
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
use itertools::Itertools;
use std::fmt::{self, Debug, Formatter};
#[derive(Clone, PartialEq, Eq)]
enum Number {
Reg(usize),
Pair(Box<Number>, Box<Number>),
}
impl Debug for Number {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
fn number_to_string(n: &Number) -> String {
match n {
Reg(v) => format!("{}", v),
Pair(a, b) => format!("[{},{}]", number_to_string(a), number_to_string(b)),
}
}
formatter.write_str(&number_to_string(self))
}
}
use Number::*;
fn pair(n1: Number, n2: Number) -> Number {
Pair(Box::new(n1), Box::new(n2))
}
fn parse(input: &str) -> Number {
fn map_json_value(value: &serde_json::Value) -> Number {
use serde_json::Value::*;
match value {
Number(n) => Reg(n.as_u64().unwrap() as usize),
Array(children) => {
assert_eq!(children.len(), 2);
pair(map_json_value(&children[0]), map_json_value(&children[1]))
}
_ => panic!("invalid number string: unexpected value type {:?}", value),
}
}
map_json_value(&serde_json::from_str(input).expect("invalid number string"))
}
fn split(n: &Number) -> Option<Number> {
match n {
Reg(num) => {
if *num > 9 {
Some(pair(Reg(num / 2), Reg(num / 2 + num % 2)))
} else {
None
}
}
Pair(n1, n2) => {
if let Some(s1) = split(&n1) {
Some(Pair(Box::new(s1), n2.clone()))
} else if let Some(s2) = split(&n2) {
Some(Pair(n1.clone(), Box::new(s2)))
} else {
None
}
}
}
}
fn explode(n: &Number) -> Option<Number> {
fn add_to_side(n: &Number, val: usize, left_side: bool) -> Number {
match n {
Reg(v) => Reg(v + val),
Pair(l, r) => match left_side {
true => Pair(Box::new(add_to_side(l, val, true)), r.clone()),
false => Pair(l.clone(), Box::new(add_to_side(r, val, false))),
},
}
}
fn unwrap_regular(n: &Number) -> usize {
match n {
Reg(v) => *v,
_ => panic!("{:?} not a regular number", n),
}
}
fn explode_layer(n: &Number, depth: usize) -> Option<(Number, usize, usize)> {
if let Pair(left, right) = n {
if depth == 4 {
return Some((Reg(0), unwrap_regular(left), unwrap_regular(right)));
}
if let Some((left, left_over, right_over)) = explode_layer(left, depth + 1) {
let right = if let Reg(n) = **right {
Reg(n + right_over)
} else {
add_to_side(right, right_over, true)
};
return Some((Pair(Box::new(left), Box::new(right)), left_over, 0));
}
if let Some((right, left_over, right_over)) = explode_layer(right, depth + 1) {
let left = if let Reg(n) = **left {
Reg(n + left_over)
} else {
add_to_side(left, left_over, false)
};
return Some((Pair(Box::new(left), Box::new(right)), 0, right_over));
}
}
None
}
explode_layer(n, 0).map(|(res, _, _)| res)
}
fn reduce(n: &Number) -> Number {
match explode(n) {
Some(exploded) => reduce(&exploded),
None => match split(n) {
Some(split) => reduce(&split),
None => n.clone(),
},
}
}
fn add(n1: &Number, n2: &Number) -> Number {
reduce(&pair(n1.clone(), n2.clone()))
}
fn magnitude(v: &Number) -> usize {
match v {
Pair(a, b) => 3 * magnitude(a) + 2 * magnitude(b),
Reg(v) => *v,
}
}
fn part1(input: &str) -> usize {
let result = input.lines().map(parse).reduce(|a, b| add(&a, &b)).unwrap();
magnitude(&result)
}
fn part2(input: &str) -> usize {
input
.lines()
.map(parse)
.permutations(2)
.map(|perm| magnitude(&add(&perm[0], &perm[1])))
.max()
.unwrap()
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test1.txt")), 3488);
assert_eq!(part1(include_str!("test2.txt")), 4140);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test2.txt")), 3993);
}
#[test]
fn test_split() {
assert_eq!(
split(&parse("[[[[0,7],4],[[7,8],[0,13]]],[1,1]]")),
Some(parse("[[[[0,7],4],[[7,8],[0,[6,7]]]],[1,1]]"))
);
assert_eq!(split(&parse("[1,2]")), None);
assert_eq!(
split(&parse(
"[[[[4,0],[5,4]],[[7,0],[15,5]]],[10,[[11,9],[11,0]]]]"
)),
Some(parse(
"[[[[4,0],[5,4]],[[7,0],[[7,8],5]]],[10,[[11,9],[11,0]]]]"
))
);
}
#[test]
fn test_magnitude() {
assert_eq!(magnitude(&parse("[[1,2],[[3,4],5]]")), 143);
assert_eq!(
magnitude(&parse(
"[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]"
)),
3488
);
}
#[test]
fn test_explode() {
assert_eq!(
explode(&parse("[[[[[9,8],1],2],3],4]")),
Some(parse("[[[[0,9],2],3],4]"))
);
assert_eq!(
explode(&parse("[7,[6,[5,[4,[3,2]]]]]")),
Some(parse("[7,[6,[5,[7,0]]]]"))
);
assert_eq!(
explode(&parse("[[3,[2,[1,[7,3]]]],[6,[5,[4,[3,2]]]]]")),
Some(parse("[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]"))
);
assert_eq!(
explode(&parse("[7,[6,[5,[4,[3,2]]]]]")),
Some(parse("[7,[6,[5,[7,0]]]]"))
);
assert_eq!(
explode(&parse("[[6,[5,[4,[3,2]]]],1]")),
Some(parse("[[6,[5,[7,0]]],3]"))
);
assert_eq!(
explode(&parse("[[3,[2,[1,[7,3]]]],[6,[5,[4,[3,2]]]]]")),
Some(parse("[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]"))
);
assert_eq!(
explode(&parse("[[3,[2,[8,0]]],[9,[5,[4,[3,2]]]]]")),
Some(parse("[[3,[2,[8,0]]],[9,[5,[7,0]]]]"))
);
assert_eq!(
explode(&parse(
"[[[[4,0],[5,4]],[[7,7],[6,5]]],[[[5,5],[0,6]],[[6,5],[5,5]]]]"
)),
None
);
}