-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
127 lines (107 loc) · 3.15 KB
/
08.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
use std::collections::{HashMap, HashSet};
use advent_of_code::Grid;
use itertools::Itertools;
advent_of_code::solution!(8);
fn record_first_antinode_locations(
antennas: &[(i64, i64)],
record: &mut HashSet<(i64, i64)>,
) {
record.extend(
antennas
.iter()
.cartesian_product(antennas)
.filter(|(l, r)| l != r)
.flat_map(|(l, r)| {
let dx = l.0 - r.0;
let dy = l.1 - r.1;
[(l.0 + dx, l.1 + dy), (r.0 - dx, r.1 - dy)]
}),
);
}
fn get_antennas(grid: &Grid<char>) -> HashMap<char, Vec<(i64, i64)>> {
let to_signed_col_row = |i| {
let (col, row) = grid.to_col_row(i);
(col as i64, row as i64)
};
grid.data
.iter()
.copied()
.enumerate()
.filter_map(|(i, c)| (c != '.').then_some((c, i)))
.map(|(c, i)| (c, to_signed_col_row(i)))
.into_group_map()
}
pub fn part_one(input: &str) -> Option<usize> {
let grid: Grid<char> = Grid::parse_lines(input);
let antennas = get_antennas(&grid);
let mut antinodes = HashSet::new();
antennas.into_values().for_each(|locations| {
record_first_antinode_locations(&locations, &mut antinodes)
});
let result = antinodes
.into_iter()
.filter(|(c, r)| *c >= 0 && *r >= 0)
.filter(|(c, r)| {
(*c as usize) < grid.width && (*r as usize) < grid.height
})
.count();
Some(result)
}
fn record_all_antinode_locations(
antennas: &[(i64, i64)],
max_col: i64,
max_row: i64,
record: &mut HashSet<(i64, i64)>,
) {
record.extend(
antennas
.iter()
.cartesian_product(antennas)
.filter(|(l, r)| l != r)
.flat_map(|(left, right)| {
let dx = left.0 - right.0;
let dy = left.1 - right.1;
[(left, (dx, dy)), (left, (-dx, -dy))]
})
.flat_map(|(left, d)| {
(0..)
.map(move |i| (left.0 + d.0 * i, left.1 + d.1 * i))
.take_while(|(col, row)| {
*col >= 0
&& *row >= 0
&& *col <= max_col
&& *row <= max_row
})
}),
);
}
pub fn part_two(input: &str) -> Option<usize> {
let grid: Grid<char> = Grid::parse_lines(input);
let antennas = get_antennas(&grid);
let mut antinodes = HashSet::new();
antennas.into_values().for_each(|locations| {
record_all_antinode_locations(
&locations,
grid.width as i64 - 1,
grid.height as i64 - 1,
&mut antinodes,
)
});
Some(antinodes.len())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result =
part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(14));
}
#[test]
fn test_part_two() {
let result =
part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(34));
}
}