-
Notifications
You must be signed in to change notification settings - Fork 0
/
ten.rs
89 lines (82 loc) · 3.48 KB
/
ten.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
use std::collections::VecDeque;
use libaoc::{Day, DayNumber};
pub fn ten() -> Day<2021> {
Day::new(
DayNumber::Ten,
|input| {
let lines = input.lines();
let mut syntax_error_score = 0usize;
lines.for_each(|line| {
let mut opened_chunks = VecDeque::new();
line.chars().for_each(|brace| match brace {
open @ ('(' | '{' | '[' | '<') => {
opened_chunks.push_front(open as u8);
}
close @ (')' | '}' | ']' | '>') => {
let top = opened_chunks.pop_front().unwrap();
// close == top + 2 is for `<>`,`[]`,`{}`; close == top + 1 for `()`
if !(close as u8 == top + 2 || close as u8 == top + 1) {
syntax_error_score += match close {
')' => 3,
']' => 57,
'}' => 1197,
'>' => 25137,
_ => unreachable!(),
};
}
}
_ => unreachable!(),
});
});
Box::new(syntax_error_score)
},
|input| {
let lines = input.lines();
let mut autocomplete_scores: Vec<usize> = lines
.filter(|line| {
let mut opened_chunks = VecDeque::new();
for brace in line.chars() {
match brace {
open @ ('(' | '{' | '[' | '<') => {
opened_chunks.push_front(open as u8);
}
close @ (')' | '}' | ']' | '>') => {
let top = opened_chunks.pop_front().unwrap();
// close == top + 2 is for `<>`,`[]`,`{}`; close == top + 1 for `()`
if !(close as u8 == top + 2 || close as u8 == top + 1) {
return false;
}
}
_ => unreachable!(),
}
}
true
})
.map(|line| {
let mut opened_chunks = VecDeque::new();
line.chars().for_each(|char| match char {
open @ ('(' | '{' | '[' | '<') => opened_chunks.push_front(open),
')' | '}' | ']' | '>' => {
opened_chunks.pop_front();
}
_ => unreachable!(),
});
opened_chunks
.iter()
.fold(0usize, |autocomplete_score, next_char| {
autocomplete_score * 5
+ match next_char {
'(' => 1,
'[' => 2,
'{' => 3,
'<' => 4,
_ => unreachable!(),
}
})
})
.collect();
autocomplete_scores.sort_unstable();
Box::new(autocomplete_scores[autocomplete_scores.len() / 2])
},
)
}