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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::collections::HashSet;
use utils::point::Point2D;
use utils::prelude::*;

/// Calculating Manhattan distance.
#[derive(Clone, Debug)]
pub struct Day01 {
    instructions: Vec<(Turn, u16)>,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum Turn {
    L,
    R,
}

impl Day01 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        Ok(Self {
            instructions: parser::literal_map!("L" => Turn::L, "R" => Turn::R)
                .then(parser::u16())
                .with_suffix(", ".or(parser::eof()))
                .parse_all(input)?,
        })
    }

    #[must_use]
    pub fn part1(&self) -> u32 {
        let mut pos = Point2D::ORIGIN;
        let mut dir = Point2D::UP;

        for &(turn, steps) in &self.instructions {
            dir = match turn {
                Turn::L => dir.turn_left(),
                Turn::R => dir.turn_right(),
            };
            pos += dir * i32::from(steps);
        }

        pos.manhattan_distance_unsigned()
    }

    #[must_use]
    pub fn part2(&self) -> u32 {
        let mut pos: Point2D<i32> = Point2D::ORIGIN;
        let mut dir = Point2D::UP;
        let mut visited = HashSet::new();

        for &(turn, steps) in &self.instructions {
            dir = match turn {
                Turn::L => dir.turn_left(),
                Turn::R => dir.turn_right(),
            };
            for _ in 0..steps {
                pos += dir;
                if !visited.insert(pos) {
                    return pos.manhattan_distance_unsigned();
                }
            }
        }

        panic!("no location visited twice");
    }
}

examples!(Day01 -> (u32, u32) [
    {input: "R2, L3", part1: 5},
    {input: "R2, R2, R2", part1: 2},
    {input: "R5, L5, R5, R3", part1: 12},
    {input: "R8, R4, R4, R8", part2: 4},
]);