-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay18.rs
162 lines (133 loc) · 4.04 KB
/
Day18.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
#![feature(if_let_guard, array_windows)]
use std::collections::HashMap;
use std::collections::VecDeque;
use std::convert::TryInto;
use std::env;
use std::io;
// Maybe indexmap would work better here?
// Or a BTreeMap with a clever keying scheme?
// Fortunately the trees are not that big, so it works fine.
type Path = VecDeque<char>;
type Tree = VecDeque<(u32, Path)>;
fn parse_tree<T>(s: T) -> Tree
where
T: AsRef<str>,
{
s.as_ref()
.chars()
.fold(
(VecDeque::new(), VecDeque::new()),
|(mut result, mut path), c| {
match c {
'[' => { path.push_back('L'); }
',' => { path.pop_back(); path.push_back('R'); }
']' => { path.pop_back(); }
// No multi-digit numbers in my input set
c if let Some(d) = c.to_digit(10) => {
result.push_back((d, path.clone()));
}
_ => unreachable!(),
}
(result, path)
},
)
.0
}
fn try_explode(t: &mut Tree) -> Result<(), ()> {
let mut to_explode = None;
for (i, (_, ap)) in t.iter().rev().skip(1).rev().enumerate() {
let (_, bp) = t.get(i + 1).unwrap();
if ap.len() == 5 && ap.iter().take(4).eq(bp.iter().take(4)) {
to_explode = Some(i);
break;
}
}
if let Some(i) = to_explode {
let (av, mut ap) = t.remove(i).unwrap();
let (bv, _) = t.remove(i).unwrap();
let left = (i as isize - 1).try_into().ok().and_then(|i| t.get_mut(i));
if let Some((v, _)) = left {
*v += av;
}
let right = t.get_mut(i);
if let Some((v, _)) = right {
*v += bv;
}
ap.pop_back();
t.insert(i, (0, ap));
Ok(())
} else {
Err(())
}
}
fn try_split(t: &mut Tree) -> Result<(), ()> {
let mut to_split = None;
for (i, (v, _)) in t.iter().enumerate() {
if *v >= 10 {
to_split = Some(i);
break;
}
}
if let Some(i) = to_split {
let (av, mut ap) = t.remove(i).unwrap();
let mut lap = ap.clone();
lap.push_back('L');
t.insert(i, (av / 2, lap));
ap.push_back('R');
t.insert(i + 1, (av - av / 2, ap));
Ok(())
} else {
Err(())
}
}
fn go_magnitude(path: &mut Path, idx: &HashMap<&Path, u32>) -> u32 {
if let Some(v) = idx.get(path) {
return *v;
} else {
path.push_back('L');
let l = go_magnitude(path, idx);
path.pop_back();
path.push_back('R');
let r = go_magnitude(path, idx);
path.pop_back();
return 3 * l + 2 * r;
}
}
fn add_trees(mut l: &mut Tree, r: &Tree) {
l.iter_mut().for_each(|(_, p)| p.push_front('L'));
let mut r = r.clone();
r.iter_mut().for_each(|(_, p)| p.push_front('R'));
l.extend(r);
while try_explode(&mut l).is_ok() || try_split(&mut l).is_ok() {}
}
fn magnitude(t: &Tree) -> u32 {
let idx: HashMap<&Path, u32> = t.iter().map(|(v, p)| (p, *v)).collect();
return go_magnitude(&mut VecDeque::new(), &idx);
}
fn main() {
let trees = io::stdin().lines().map(Result::unwrap).map(parse_tree);
if env::args().nth(1).unwrap() == "1" {
let result = trees
.into_iter()
.reduce(|mut l, r| {
add_trees(&mut l, &r);
while try_explode(&mut l).is_ok() || try_split(&mut l).is_ok() {}
l
})
.unwrap();
println!("{}", magnitude(&result));
} else {
let mut max = 0;
let trees: Vec<_> = trees.collect();
for i in 0..trees.len() {
for j in i + 1..trees.len() {
for (a, b) in [(i, j), (j, i)] {
let mut first = trees[a].clone();
add_trees(&mut first, &trees[b]);
max = max.max(magnitude(&first));
}
}
}
println!("{}", max);
}
}