-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
119 lines (105 loc) · 3.13 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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> u32 {
let mut data = data.as_bytes();
let mut ch;
let mut level = 0_u32;
let mut score = 0_u32;
while !data.is_empty() {
(ch, data) = data.split_first().unwrap();
match ch {
b'{' => level += 1,
b'<' => data = strip_garbage(data),
b'}' => {
score += level;
level -= 1;
}
_ => {}
}
}
score
}
fn part_two(data: &str) -> u32 {
let mut data = data.as_bytes();
let mut ch;
let mut score = 0_u32;
while !data.is_empty() {
(ch, data) = data.split_first().unwrap();
if ch == &b'<' {
let (count, new) = count_garbage(data);
score += count;
data = new;
}
}
score
}
fn strip_garbage(input: &[u8]) -> &[u8] {
let mut escaped = false;
let mut data = input;
let mut ch;
while !data.is_empty() {
(ch, data) = data.split_first().unwrap();
match (escaped, ch) {
(true, _) => escaped = false,
(false, b'!') => escaped = true,
(false, b'>') => return data,
(_, _) => {}
}
}
unreachable!()
}
fn count_garbage(input: &[u8]) -> (u32, &[u8]) {
let mut escaped = false;
let mut data = input;
let mut ch;
let mut count = 0;
while !data.is_empty() {
(ch, data) = data.split_first().unwrap();
match (escaped, ch) {
(true, _) => escaped = false,
(false, b'!') => escaped = true,
(false, b'>') => return (count, data),
(_, _) => count += 1,
}
}
unreachable!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
assert_eq!(1, part_one("{}"));
assert_eq!(6, part_one("{{{}}}"));
assert_eq!(5, part_one("{{},{}}"));
assert_eq!(16, part_one("{{{},{},{{}}}}"));
assert_eq!(1, part_one("{<a>,<a>,<a>,<a>}"));
assert_eq!(9, part_one("{{<ab>},{<ab>},{<ab>},{<ab>}}"));
assert_eq!(9, part_one("{{<!!>},{<!!>},{<!!>},{<!!>}}"));
assert_eq!(3, part_one("{{<a!>},{<a!>},{<a!>},{<ab>}}"));
}
#[test]
fn two() {
assert_eq!(0, part_two("<>"));
assert_eq!(17, part_two("<random characters>"));
assert_eq!(3, part_two("<<<<>"));
assert_eq!(2, part_two("<{!>}>"));
assert_eq!(0, part_two("<!!>"));
assert_eq!(0, part_two("<!!!>>"));
assert_eq!(10, part_two("<{o\"i!a,<{i<a>"));
}
#[test]
fn test_strip_garbage() {
assert!(strip_garbage(b"<>").is_empty());
assert!(strip_garbage(b"<random characters>").is_empty());
assert!(strip_garbage(b"<<<<>").is_empty());
assert!(strip_garbage(b"<{!>}>").is_empty());
assert!(strip_garbage(b"<!!>").is_empty());
assert!(strip_garbage(b"<!!!>>").is_empty());
assert!(strip_garbage(b"<{o\"i!a,<{i<a>").is_empty());
}
}