generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.rs
175 lines (144 loc) · 4.49 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
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
advent_of_code::solution!(8);
struct Map {
width: usize,
height: usize,
antennas: Vec<Antenna>,
}
struct Antenna {
x: i32,
y: i32,
id: u32,
frequency: char,
}
impl Antenna {
fn new(id: u32, frequency: char, x: i32, y: i32) -> Self {
Antenna {
id,
frequency,
x,
y,
}
}
}
fn process_map(input: &str) -> Map {
let mut antennas = Vec::new();
let width = input.lines().last().unwrap().chars().count();
let height = input.lines().count();
// Find antennas
let mut serial = 0;
for (y, line) in input.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
match c {
val if val.is_alphanumeric() => {
let x: i32 = x.try_into().unwrap();
let y: i32 = y.try_into().unwrap();
let antenna = Antenna::new(serial, val, x, y);
antennas.push(antenna);
serial += 1;
}
_ => {}
}
}
}
Map {
width,
height,
antennas,
}
}
fn in_bounds(x: i32, y: i32, map: &Map) -> bool {
x >= 0 && x < map.width.try_into().unwrap() && y >= 0 && y < map.height.try_into().unwrap()
}
fn find_antinodes(map: &Map) -> Vec<(i32, i32)> {
let mut antinodes = Vec::new();
let antennas = &map.antennas;
for antenna in antennas.iter() {
let resonant_antennas = antennas
.iter()
.filter(|a| a.frequency == antenna.frequency && a.id != antenna.id);
for reso in resonant_antennas {
let dx = antenna.x - reso.x;
let dy = antenna.y - reso.y;
let antinode = (antenna.x + dx, antenna.y + dy);
if in_bounds(antinode.0, antinode.1, map) {
if antinodes
.iter()
.any(|an: &(i32, i32)| an.0 == antinode.0 && an.1 == antinode.1)
{
continue;
}
antinodes.push(antinode);
// println!("{:?}", antinode);
}
}
}
antinodes
}
fn find_antinodes_2(map: &Map) -> Vec<(i32, i32)> {
let mut antinodes = Vec::new();
let antennas = &map.antennas;
for antenna in antennas.iter() {
let resonant_antennas = antennas
.iter()
.filter(|a| a.frequency == antenna.frequency && a.id != antenna.id);
for reso in resonant_antennas {
let dx = antenna.x - reso.x;
let dy = antenna.y - reso.y;
let mut antinode = (antenna.x + dx, antenna.y + dy);
loop {
if in_bounds(antinode.0, antinode.1, map) {
if !antinodes
.iter()
.any(|an: &(i32, i32)| an.0 == antinode.0 && an.1 == antinode.1)
{
antinodes.push(antinode);
// println!("{:?}", antinode);
}
} else {
break;
}
antinode = (antinode.0 + dx, antinode.1 + dy);
}
antinode = (antenna.x - dx, antenna.y - dy);
loop {
if in_bounds(antinode.0, antinode.1, map) {
if !antinodes
.iter()
.any(|an: &(i32, i32)| an.0 == antinode.0 && an.1 == antinode.1)
{
antinodes.push(antinode);
// println!("{:?}", antinode);
}
} else {
break;
}
antinode = (antinode.0 - dx, antinode.1 - dy);
}
}
}
antinodes
}
pub fn part_one(input: &str) -> Option<u32> {
let antennas = process_map(input);
let antinodes = find_antinodes(&antennas);
Some(antinodes.len().try_into().unwrap())
}
pub fn part_two(input: &str) -> Option<u32> {
let antennas = process_map(input);
let antinodes = find_antinodes_2(&antennas);
Some(antinodes.len().try_into().unwrap())
}
#[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));
}
}