1use aoc_helper::{AocDay, Puzzle};
2
3fn first_instruction_that_sends_to_basement(instructions: String) -> usize {
5 let mut floor = 0;
6 for (i, instruction) in instructions.chars().enumerate() {
7 match instruction {
8 '(' => floor += 1,
9 _ => floor -= 1,
10 }
11 if floor < 0 {
12 return i + 1;
13 }
14 }
15 unreachable!();
16}
17
18fn main() {
19 let mut day_1 = AocDay::new(2015, 1);
23
24 let part_1 = Puzzle::new(1, |instructions: String| {
26 let mut floor = 0;
27 instructions.chars().for_each(|x| if x == '(' { floor += 1 } else { floor -= 1 });
28 floor
29 })
30 .with_examples(&["(())", "()()", ")))", ")())())"]);
31
32 let part_2 = Puzzle::new(2, first_instruction_that_sends_to_basement)
34 .with_examples(&[")", "()())"]);
35
36 day_1.test(&part_1);
38 day_1.test(&part_2);
39 day_1.run(&part_1).unwrap();
41 day_1.run(&part_2).unwrap();
42}