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
use utils::prelude::*;

/// Counting brackets.
#[derive(Clone, Debug)]
pub struct Day01 {
    directions: Vec<i32>,
}

impl Day01 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        Ok(Self {
            directions: input
                .chars()
                .map(|c| match c {
                    '(' => Ok(1),
                    ')' => Ok(-1),
                    _ => Err(InputError::new(input, c, "expected bracket")),
                })
                .collect::<Result<Vec<i32>, InputError>>()?,
        })
    }

    #[must_use]
    pub fn part1(&self) -> i32 {
        self.directions.iter().sum()
    }

    #[must_use]
    pub fn part2(&self) -> usize {
        self.directions
            .iter()
            .enumerate()
            .scan(0, |floor, (i, x)| {
                *floor += x;
                Some((i + 1, *floor)) // Character positions are 1-indexed in the puzzle
            })
            .find(|&(_, floor)| floor == -1)
            .expect("floor -1 not reached")
            .0
    }
}

examples!(Day01 -> (i32, usize) [
    {input: "(())", part1: 0},
    {input: "()()", part1: 0},
    {input: "(((", part1: 3},
    {input: "(()(()(", part1: 3},
    {input: "))(((((", part1: 3},
    {input: "())", part1: -1},
    {input: "))(", part1: -1},
    {input: ")))", part1: -3},
    {input: ")())())", part1: -3},
    {input: ")", part2: 1},
    {input: "()())", part2: 5},
]);