forked from maneatingape/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.rs
32 lines (28 loc) · 729 Bytes
/
day01.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
//! # Not Quite Lisp
//!
//! The input is first converted into bytes. This is safe as it contains only ASCII characters.
//! Then each parenthesis is parsed into either +1 or -1, treating the trailing newline
//! as a special case of 0.
pub fn parse(input: &str) -> Vec<i32> {
fn helper(b: u8) -> i32 {
match b {
b'(' => 1,
b')' => -1,
_ => 0,
}
}
input.bytes().map(helper).collect()
}
pub fn part1(input: &[i32]) -> i32 {
input.iter().sum()
}
pub fn part2(input: &[i32]) -> usize {
let mut floor = 0;
for (i, x) in input.iter().enumerate() {
floor += x;
if floor < 0 {
return i + 1;
}
}
unreachable!()
}