2015_day1/
2015_day1.rs

1use aoc_helper::{AocDay, Puzzle};
2
3// The solver function for part 1
4fn 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    // NOTE: You need to specify a session ID for this to work
20
21    // Create a new `AocDay` instance for day 1 of aoc 2015
22    let mut day_1 = AocDay::new(2015, 1);
23
24    // Create a new `Puzzle` instance for part 1
25    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    // Create a new `Puzzle` instance for part 2
33    let part_2 = Puzzle::new(2, first_instruction_that_sends_to_basement)
34        .with_examples(&[")", "()())"]);
35
36    // Test the example cases
37    day_1.test(&part_1);
38    day_1.test(&part_2);
39    // Run the day's input
40    day_1.run(&part_1).unwrap();
41    day_1.run(&part_2).unwrap();
42}