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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! This is my solution for [Advent of Code - Day 9 - _Rope Bridge_](https://adventofcode.com/2022/day/9)
//!
//!

use std::fs;
use itertools::Itertools;
use crate::day_9::Direction::*;

/// Represent the directions a motion can be in
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
enum Direction {
    UP,
    DOWN,
    LEFT,
    RIGHT,
}

/// An instruction to move the head of the rope in a direction for a distance
type Motion = (Direction, usize);
type Position = (isize, isize);

/// The entry point for running the solutions with the 'real' puzzle input.
///
/// - The puzzle input is expected to be at `<project_root>/res/day-9-input`
/// - It is expected this will be called by [`super::main()`] when the user elects to run day 9.
pub fn run() {
    let contents =
        fs::read_to_string("res/day-9-input").expect("Failed to read file");

    let motions = parse_input(&contents);

    println!(
        "The tail of the rope with one knot passes through {} unique positions",
        count_tail_positions(&motions, 1)
    );

    println!(
        "The tail of the rope with 9 knots passes through {} unique positions",
        count_tail_positions(&motions, 9)
    );
}

/// Map the input file to the internal representation
fn parse_input(input: &String) -> Vec<Motion> {
    input.lines().map(parse_motion).collect()
}

/// Map a line of the input to the internal representation
fn parse_motion(line: &str) -> Motion {
    let (letter, number) = line.split_once(" ").unwrap();

    let direction = match letter {
        "U" => UP,
        "D" => DOWN,
        "L" => LEFT,
        "R" => RIGHT,
        _ => unreachable!()
    };

    let distance = number.parse().unwrap();

    (direction, distance)
}

/// Map a specification of a move of the head of the rope to the list of positions it follows
fn apply_motion((x, y): Position, (direction, distance): Motion) -> Vec<Position> {
    let mut positions = Vec::new();
    for d in 1..=distance {
        let d_i = isize::try_from(d).unwrap();
        positions.push(
            match direction {
                UP => (x, y - d_i),
                DOWN => (x, y + d_i),
                LEFT => (x - d_i, y),
                RIGHT => (x + d_i, y),
            }
        )
    }

    positions
}

/// Give a new head position, move the tail so it is still touching
fn update_tail((head_x, head_y): Position, (tail_x, tail_y): Position) -> Position {
    if (head_x - tail_x).abs() <= 1 && (head_y - tail_y).abs() <= 1 {
        (tail_x, tail_y)
    } else {
        (
            if tail_x < head_x { tail_x + 1 } else if tail_x > head_x { tail_x - 1 } else { tail_x },
            if tail_y < head_y { tail_y + 1 } else if tail_y > head_y { tail_y - 1 } else { tail_y },
        )
    }
}

/// map a list of motions specifications to the list of positions it follows
fn apply_motions(origin: Position, motions: &Vec<Motion>) -> Vec<Position> {
    let mut positions = Vec::new();
    positions.push(origin);

    for &motion in motions {
        apply_motion(*positions.last().unwrap(), motion)
            .iter()
            .for_each(|&pos| positions.push(pos));
    }

    positions
}

// Map the positions the previous section of a rope follows to the positions the next section follows
fn follow_head(origin: Position, head_positions: Vec<Position>) -> Vec<Position> {
    let mut tail_positions = Vec::new();
    tail_positions.push(origin);

    for head_position in head_positions {
        tail_positions.push(
            update_tail(
                head_position,
                *tail_positions.last().unwrap(),
            )
        );
    }

    tail_positions
}

/// Map the movement specification to the tail movement of an arbitrary length of rope.
fn count_tail_positions(head_motions: &Vec<Motion>, rope_length: usize) -> usize {
    (0..rope_length)
        .fold(
            apply_motions((0, 0), head_motions),
            |previous_knot, _| follow_head((0, 0), previous_knot),
        )
        .iter().unique().count()
}

#[cfg(test)]
mod tests {
    use crate::day_9::Direction::*;
    use crate::day_9::{apply_motion, apply_motions, count_tail_positions, follow_head, Motion, parse_input, update_tail};

    fn sample_motions() -> Vec<Motion> {
        vec![
            (RIGHT, 4),
            (UP, 4),
            (LEFT, 3),
            (DOWN, 1),
            (RIGHT, 4),
            (DOWN, 1),
            (LEFT, 5),
            (RIGHT, 2),
        ]
    }

    #[test]
    fn can_parse() {
        let input = "R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2".to_string();

        assert_eq!(parse_input(&input), sample_motions());
    }

    #[test]
    fn can_move_head() {
        assert_eq!(
            apply_motion((0, 0), (RIGHT, 4)),
            vec![(1, 0), (2, 0), (3, 0), (4, 0)]
        );

        assert_eq!(
            apply_motion((4, 0), (UP, 2)),
            vec![(4, -1), (4, -2)]
        )
    }

    #[test]
    fn can_apply_motions() {
        assert_eq!(
            apply_motions((0, 0), &vec![(RIGHT, 4), (UP, 2)]),
            vec![(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, -1), (4, -2)]
        );
    }

    #[test]
    fn can_update_tail_for_head() {
        assert_eq!(update_tail((0, 0), (0, 0)), (0, 0));
        assert_eq!(update_tail((0, 1), (0, 0)), (0, 0));
        assert_eq!(update_tail((1, 1), (0, 0)), (0, 0));
        assert_eq!(update_tail((2, 2), (0, 0)), (1, 1));
        assert_eq!(update_tail((-2, 0), (0, 0)), (-1, 0));
        assert_eq!(update_tail((-2, 1), (0, 0)), (-1, 1));
    }

    #[test]
    fn can_follow_head() {
        assert_eq!(
            follow_head(
                (0, 0),
                vec![(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, -1), (4, -2)],
            ),
            vec![(0, 0), (0, 0), (0, 0), (1, 0), (2, 0), (3, 0), (3, 0), (4, -1)]
        )
    }

    #[test]
    fn can_count_tail_locations() {
        assert_eq!(count_tail_positions(&sample_motions(), 1), 13);
        assert_eq!(count_tail_positions(&sample_motions(), 9), 1);

        let larger_input = "R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20".to_string();
        let larger_example = parse_input(&larger_input);
        assert_eq!(count_tail_positions(&larger_example, 9), 36);

    }
}