advent_of_code/year2017/
day09.rs1use crate::input::Input;
2
3pub fn solve(input: &Input) -> Result<u32, String> {
4 let mut result = 0;
5 let mut stack = Vec::new();
6 let mut ignore_next = false;
7 let mut inside_garbage = false;
8
9 for char in input.text.bytes() {
10 if ignore_next {
11 ignore_next = false;
12 continue;
13 }
14
15 match (inside_garbage, char) {
16 (false, b'<') => {
17 inside_garbage = true;
18 }
19 (false, b'{') => {
20 if input.is_part_one() {
21 let this_score = 1 + stack.last().unwrap_or(&0);
22 stack.push(this_score);
23 result += this_score;
24 }
25 }
26 (false, b'}') => {
27 stack.pop();
28 }
29 (true, b'!') => {
30 ignore_next = true;
31 }
32 (true, b'>') => {
33 inside_garbage = false;
34 }
35 (true, _) => {
36 if input.is_part_two() {
37 result += 1;
38 }
39 }
40 _ => {}
41 }
42 }
43 Ok(result)
44}
45
46#[test]
47fn tests() {
48 let real_input = include_str!("day09_input.txt");
49 test_part_one!(real_input => 11089);
50 test_part_two!(real_input => 5288);
51}