1use utils::prelude::*;
2
3#[derive(Clone, Debug)]
5pub struct Day01 {
6 part1: u32,
7 part2: u32,
8}
9
10parser::parsable_enum! {
11 #[derive(Clone, Copy, Debug)]
12 enum Direction {
13 "L" => Left = -1,
14 "R" => Right = 1,
15 }
16}
17
18impl Day01 {
19 pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
20 let mut pos = 50i32;
21 let (mut part1, mut part2) = (0, 0);
22 for line in Direction::PARSER
23 .then(parser::number_range(1i32..=999))
24 .with_eol()
25 .parse_iterator(input)
26 {
27 let (dir, steps) = line?;
28
29 part2 += match dir {
30 Direction::Left => ((100 - pos + steps) / 100) as u32 - u32::from(pos == 0),
31 Direction::Right => ((pos + steps) / 100) as u32,
32 };
33
34 pos = (pos + (steps * dir as i32)).rem_euclid(100);
35 part1 += u32::from(pos == 0);
36 }
37
38 Ok(Self { part1, part2 })
39 }
40
41 #[must_use]
42 pub fn part1(&self) -> u32 {
43 self.part1
44 }
45
46 #[must_use]
47 pub fn part2(&self) -> u32 {
48 self.part2
49 }
50}
51
52examples!(Day01 -> (u32, u32) [
53 {input: "L68\nL30\nR48\nL5\nR60\nL55\nL1\nL99\nR14\nL82", part1: 3, part2: 6},
54]);