-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.rs
77 lines (64 loc) · 1.71 KB
/
day10.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
//! [Day 10: Syntax Scoring](https://adventofcode.com/2021/day/10)
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u64, u64) {
let mut part1 = 0;
let mut part2 = vec![];
for line in data.lines() {
let (corrupted, completed) = check(line);
part1 += corrupted;
if completed != 0 {
part2.push(completed);
}
}
// part2.sort_by(|a, b| a.cmp(b));
part2.sort_unstable();
(part1, part2[part2.len() / 2])
}
fn check(line: &str) -> (u64, u64) {
let mut stack = vec![];
for c in line.chars() {
match c {
'(' => stack.push(')'),
'[' => stack.push(']'),
'{' => stack.push('}'),
'<' => stack.push('>'),
_ => {
let d = stack.pop().unwrap();
if c != d {
match c {
')' => return (3, 0),
']' => return (57, 0),
'}' => return (1197, 0),
'>' => return (25137, 0),
_ => return (0, 0),
}
}
}
}
}
let mut score = 0u64;
while let Some(d) = stack.pop() {
match d {
')' => score = score * 5 + 1,
']' => score = score * 5 + 2,
'}' => score = score * 5 + 3,
'>' => score = score * 5 + 4,
_ => score *= 5,
}
}
(0, score)
}
#[cfg(test)]
mod test {
use super::*;
const TEST_INPUT: &str = include_str!("test.txt");
#[test]
fn test_solve() {
assert_eq!(solve(TEST_INPUT), (26397, 288957));
}
}