advent_of_code_2024/
day_8.rs

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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//! This is my solution for [Advent of Code - Day 8: _Resonant Collinearity_](https://adventofcode.com/2024/day/8)
//!
//! [`parse_input`] converts the input file to an [`AntennaMap`] which stores a [`HashMap`] of frequency to list of
//! antenna coordinates, as well as the upper bounds of the grid.
//!
//! [`count_antinodes_for_map`] solves both parts, breaking the work down into calling
//! [`find_antinodes_for_frequency`] for each frequency in the map. This in turn uses [`find_antinodes_for_pair`] on
//! each combination of antenna in the frequency group.
//!
//! [`sequence_from_antenna`] extrapolates the line defined by a pair of antenna, in one direction and
//! [`antinode_pair_sequence_modifier`] and [`resonant_harmonies_sequence_modifier`] handle selecting the right
//! node(s) for part 1 and 2 respectively. [`find_antinodes_for_pair`] uses [`sequence_from_antenna`] starting from
//! each node in the pair.

use itertools::{iterate, Itertools};
use std::collections::HashMap;
use std::fs;

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

    println!(
        "There are {} unique antinodes",
        count_antinodes_for_map(&antenna_map, antinode_pair_sequence_modifier)
    );

    println!(
        "There are {} unique antinodes",
        count_antinodes_for_map(&antenna_map, resonant_harmonies_sequence_modifier)
    );
}

/// A coordinate on the grid
type Coordinate = (usize, usize);

/// Represent the puzzle grid by its upper bounds and the position of antenna grouped by frequency
#[derive(Eq, PartialEq, Debug)]
struct AntennaMap {
    height: usize,
    width: usize,
    antenna: HashMap<char, Vec<Coordinate>>,
}

/// Converts the text input into the internal representation
fn parse_input(input: &String) -> AntennaMap {
    let mut lines = input.lines();
    let width = lines.next().unwrap().len();
    let height = lines.count() + 1;
    let mut antenna: HashMap<char, Vec<Coordinate>> = HashMap::new();

    for (row, line) in input.lines().enumerate() {
        for (col, char) in line.chars().enumerate() {
            if char != '.' {
                antenna.entry(char).or_default().push((row, col))
            }
        }
    }

    AntennaMap {
        width,
        height,
        antenna,
    }
}

/// This differentiates the two parts by allowing outside control over which nodes are selected when extrapolating
/// the line between two antenna
type SequenceModifier = fn(Vec<Coordinate>) -> Vec<Coordinate>;

/// Extrapolate from a point along a delta whilst it's within the bounds of the antenna map
fn sequence_from_antenna(
    (r, c): Coordinate,
    (dr, dc): (isize, isize),
    (height, width): &(usize, usize),
) -> Vec<Coordinate> {
    iterate(0, |i| i + 1)
        .map(move |i| {
            r.checked_add_signed(i * dr)
                .zip(c.checked_add_signed(i * dc))
                .filter(|(r, c)| r < height && c < width)
        })
        .while_some()
        .collect()
}

/// Fine the antinodes by determining the coordinate delta between two antinodes, extrapolating the line from both
/// ends, applying the SequenceModifier relevant to the part being solved.
fn find_antinodes_for_pair(
    (r1, c1): Coordinate,
    (r2, c2): Coordinate,
    bounds: &(usize, usize),
    sequence_modifier: SequenceModifier,
) -> Vec<Coordinate> {
    let dr = r1 as isize - r2 as isize;
    let dc = c1 as isize - c2 as isize;

    let increasing = sequence_from_antenna((r1, c1).clone(), (dr, dc).clone(), bounds);
    let decreasing = sequence_from_antenna((r2, c2), (-dr, -dc), bounds);

    [sequence_modifier(increasing), sequence_modifier(decreasing)].concat()
}

/// Part 1 - Select only the first node beyond the origin
fn antinode_pair_sequence_modifier(coordinate_sequence: Vec<Coordinate>) -> Vec<Coordinate> {
    coordinate_sequence
        .into_iter()
        .dropping(1)
        .take(1)
        .collect()
}

/// Part 2 - Select all nodes including the origin - essentially the identity function
fn resonant_harmonies_sequence_modifier(coordinate_sequence: Vec<Coordinate>) -> Vec<Coordinate> {
    coordinate_sequence
}

/// Combine all pairs of antenna in a frequency and return the unique antinodes
fn find_antinodes_for_frequency(
    antenna: &Vec<Coordinate>,
    bounds: &(usize, usize),
    sequence_modifier: SequenceModifier,
) -> Vec<Coordinate> {
    antenna
        .iter()
        .tuple_combinations()
        .flat_map(|(a1, a2)| find_antinodes_for_pair(*a1, *a2, bounds, sequence_modifier))
        .unique()
        .collect()
}

/// For all frequencies un the map, find all the antinodes, then count unique coordinates.
fn count_antinodes_for_map(antenna_map: &AntennaMap, sequence_modifier: SequenceModifier) -> usize {
    let bounds = (antenna_map.height, antenna_map.width);
    antenna_map
        .antenna
        .values()
        .flat_map(|antenna| find_antinodes_for_frequency(antenna, &bounds, sequence_modifier))
        .unique()
        .count()
}

#[cfg(test)]
mod tests {
    use crate::day_8::*;
    use crate::helpers::test::assert_contains_in_any_order;
    
    #[test]
    fn can_parse_input() {
        let input = "............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............"
            .to_string();

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

    fn example_map() -> AntennaMap {
        AntennaMap {
            height: 12,
            width: 12,
            antenna: vec![
                ('0', vec![(1, 8), (2, 5), (3, 7), (4, 4)]),
                ('A', vec![(5, 6), (8, 8), (9, 9)]),
            ]
            .into_iter()
            .collect(),
        }
    }

    #[test]
    fn can_find_antinodes_for_pair() {
        assert_contains_in_any_order(
            find_antinodes_for_pair((3, 4), (5, 5), &(12, 12), antinode_pair_sequence_modifier),
            vec![(1, 3), (7, 6)],
        );
        assert_contains_in_any_order(
            find_antinodes_for_pair((4, 8), (5, 5), &(12, 12), antinode_pair_sequence_modifier),
            vec![(6, 2), (3, 11)],
        );
        assert_contains_in_any_order(
            find_antinodes_for_pair((4, 8), (5, 5), &(10, 10), antinode_pair_sequence_modifier),
            vec![(6, 2)],
        );
        assert_contains_in_any_order(
            find_antinodes_for_pair((1, 1), (3, 3), &(10, 10), antinode_pair_sequence_modifier),
            vec![(5, 5)],
        );
    }

    #[test]
    fn can_find_antinodes_for_pair_with_resonant_harmonics() {
        assert_contains_in_any_order(
            find_antinodes_for_pair(
                (2, 3),
                (3, 5),
                &(10, 10),
                resonant_harmonies_sequence_modifier,
            ),
            vec![(1, 1), (2, 3), (3, 5), (4, 7), (5, 9)],
        );
        assert_contains_in_any_order(
            find_antinodes_for_pair(
                (4, 3),
                (3, 5),
                &(10, 10),
                resonant_harmonies_sequence_modifier,
            ),
            vec![(5, 1), (4, 3), (3, 5), (2, 7), (1, 9)],
        );
    }

    #[test]
    fn can_find_antinodes_for_frequency() {
        assert_contains_in_any_order(
            find_antinodes_for_frequency(
                &vec![(3, 4), (4, 8), (5, 5)],
                &(10, 10),
                antinode_pair_sequence_modifier,
            ),
            vec![(1, 3), (7, 6), (6, 2), (2, 0)],
        );
    }

    #[test]
    fn can_find_antinodes_for_frequency_with_resonant_harmonics() {
        assert_contains_in_any_order(
            find_antinodes_for_frequency(
                &vec![(0, 0), (1, 3), (2, 1)],
                &(10, 10),
                resonant_harmonies_sequence_modifier,
            ),
            vec![
                (0, 0),
                (0, 5),
                (1, 3),
                (2, 1),
                (2, 6),
                (3, 9),
                (4, 2),
                (6, 3),
                (8, 4),
            ],
        );
    }

    #[test]
    fn can_count_antinodes_for_map() {
        assert_eq!(
            count_antinodes_for_map(&example_map(), antinode_pair_sequence_modifier),
            14
        );
        assert_eq!(
            count_antinodes_for_map(&example_map(), resonant_harmonies_sequence_modifier),
            34
        );
    }
}