-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.rs
62 lines (57 loc) · 1.71 KB
/
10.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
#![feature(test)]
type Input = Vec<String>;
fn setup(input: &str) -> Input {
input.lines().map(|s| s.to_string()).collect()
}
fn part1(input: &Input) -> String {
input
.iter()
.map(|line| {
let mut stack = Vec::new();
for c in line.chars() {
if "([{<".contains(c) {
stack.push(c);
} else {
let x = stack.pop().unwrap();
if !["()", "[]", "{}", "<>"].contains(&format!("{}{}", x, c).as_str()) {
return match c {
')' => 3,
']' => 57,
'}' => 1197,
'>' => 25137,
_ => panic!(),
};
}
}
}
0
})
.sum::<u32>()
.to_string()
}
fn part2(input: &Input) -> String {
let mut scores: Vec<usize> = input
.iter()
.filter_map(|line| {
let mut stack = Vec::new();
for c in line.chars() {
if "([{<".contains(c) {
stack.push(c);
} else {
let x = stack.pop().unwrap();
if !["()", "[]", "{}", "<>"].contains(&format!("{}{}", x, c).as_str()) {
return None;
}
}
}
let mut out = 0;
for c in stack.iter().rev() {
out = out * 5 + " ([{<".find(*c).unwrap();
}
Some(out)
})
.collect();
scores.sort();
scores[scores.len() / 2].to_string()
}
aoc::main!(2021, 10, ex: 1);