aoc/year2015/
day01.rs

1//! # Not Quite Lisp
2//!
3//! The input is first converted into bytes. This is safe as it contains only ASCII characters.
4//! Then each parenthesis is parsed into either +1 or -1, treating the trailing newline
5//! as a special case of 0.
6pub fn parse(input: &str) -> Vec<i32> {
7    fn helper(b: u8) -> i32 {
8        match b {
9            b'(' => 1,
10            b')' => -1,
11            _ => 0,
12        }
13    }
14    input.bytes().map(helper).collect()
15}
16
17pub fn part1(input: &[i32]) -> i32 {
18    input.iter().sum()
19}
20
21pub fn part2(input: &[i32]) -> usize {
22    let mut floor = 0;
23
24    for (i, x) in input.iter().enumerate() {
25        floor += x;
26        if floor < 0 {
27            return i + 1;
28        }
29    }
30
31    unreachable!()
32}