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.
5pub fn parse(input: &str) -> Vec<i32> {
6 input.trim().bytes().map(|b| if b == b'(' { 1 } else { -1 }).collect()
7}
8
9pub fn part1(input: &[i32]) -> i32 {
10 input.iter().sum()
11}
12
13pub fn part2(input: &[i32]) -> usize {
14 let mut floor = 0;
15 input
16 .iter()
17 .position(|&b| {
18 floor += b;
19 floor < 0
20 })
21 .map(|i| i + 1)
22 .unwrap()
23}