Skip to content

Commit 452adc1

Browse files
committed
Working Problem 8 Part 2 for AOC2024
1 parent 8531aa2 commit 452adc1

File tree

1 file changed

+51
-70
lines changed

1 file changed

+51
-70
lines changed

aoc08/src/main.rs

+51-70
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,44 @@ struct Graph {
2424
}
2525

2626
impl Graph {
27-
fn find_antinodes(&mut self) -> usize {
27+
fn inbounds(&self, coord: Coord) -> bool {
28+
coord.0 >= 0
29+
&& coord.0 < self.width as isize
30+
&& coord.1 >= 0
31+
&& coord.1 < self.height as isize
32+
}
33+
34+
fn compute_antinodes(&self, left: Coord, right: Coord, harmonics: bool) -> Vec<Coord> {
35+
let mut nodes = Vec::new();
36+
let mut coord = left;
37+
let diff = (left.0 - right.0, left.1 - right.1);
38+
if harmonics {
39+
while self.inbounds(coord) {
40+
nodes.push(coord);
41+
coord = (coord.0 + diff.0, coord.1 + diff.1);
42+
}
43+
} else {
44+
coord = (coord.0 + diff.0, coord.1 + diff.1);
45+
if self.inbounds(coord) {
46+
nodes.push(coord);
47+
}
48+
}
49+
50+
nodes
51+
}
52+
53+
fn find_antinodes(&mut self, harmonics: bool) -> usize {
2854
let mut antinodes = HashSet::new();
2955
for locs in self.antennas.values() {
3056
for (idx, &left) in locs.iter().enumerate() {
3157
for &right in locs.iter().skip(idx + 1) {
32-
let nodes = compute_antinodes(left, right);
58+
let nodes = self.compute_antinodes(left, right, harmonics);
59+
for node in nodes {
60+
antinodes.insert(node);
61+
}
62+
let nodes = self.compute_antinodes(right, left, harmonics);
3363
for node in nodes {
34-
if node.0 >= 0
35-
&& node.0 < self.width as isize
36-
&& node.1 >= 0
37-
&& node.1 < self.height as isize
38-
{
39-
antinodes.insert(node);
40-
}
64+
antinodes.insert(node);
4165
}
4266
}
4367
}
@@ -64,63 +88,6 @@ impl Graph {
6488
}
6589
}
6690

67-
// None of these are bounds-checked. We do that in the caller.
68-
fn compute_antinodes(left: Coord, right: Coord) -> Vec<Coord> {
69-
let mut nodes = Vec::new();
70-
let x_diff = (left.0 - right.0).abs();
71-
let y_diff = (left.1 - right.1).abs();
72-
if left.0 < right.0 {
73-
if left.1 < right.1 {
74-
// L.
75-
// .R
76-
let x = left.0 - x_diff;
77-
let y = left.1 - y_diff;
78-
nodes.push((x, y));
79-
80-
let x = right.0 + x_diff;
81-
let y = right.1 + y_diff;
82-
nodes.push((x, y));
83-
} else {
84-
// left.1 >= right.1
85-
// .R
86-
// L.
87-
let x = left.0 - x_diff;
88-
let y = left.1 + y_diff;
89-
nodes.push((x, y));
90-
91-
let x = right.0 + x_diff;
92-
let y = right.1 - y_diff;
93-
nodes.push((x, y));
94-
}
95-
} else {
96-
// left.0 >= right.0
97-
if left.1 < right.1 {
98-
// .L
99-
// R.
100-
let x = right.0 - x_diff;
101-
let y = right.1 + y_diff;
102-
nodes.push((x, y));
103-
104-
let x = left.0 + x_diff;
105-
let y = left.1 - y_diff;
106-
nodes.push((x, y));
107-
} else {
108-
// left.1 >= right.1
109-
// R.
110-
// .L
111-
let x = right.0 - x_diff;
112-
let y = right.1 - y_diff;
113-
nodes.push((x, y));
114-
115-
let x = left.0 + x_diff;
116-
let y = left.1 + y_diff;
117-
nodes.push((x, y));
118-
}
119-
}
120-
121-
nodes
122-
}
123-
12491
fn read_graph(lines: &[String]) -> Graph {
12592
let mut antennas: HashMap<char, Vec<Coord>> = HashMap::new();
12693
let height = lines.len();
@@ -149,17 +116,31 @@ fn read_graph(lines: &[String]) -> Graph {
149116

150117
#[test]
151118
fn test_prelim() {
152-
let antinodes = read_graph(&get_input("prelim.txt")).find_antinodes();
119+
let antinodes = read_graph(&get_input("prelim.txt")).find_antinodes(false);
153120
assert_eq!(antinodes, 14);
154121
}
155122

156123
#[test]
157124
fn test_part1() {
158-
let antinodes = read_graph(&get_input("input.txt")).find_antinodes();
125+
let antinodes = read_graph(&get_input("input.txt")).find_antinodes(false);
159126
assert_eq!(antinodes, 344);
160127
}
161128

129+
#[test]
130+
fn test_prelim2() {
131+
let antinodes = read_graph(&get_input("prelim.txt")).find_antinodes(true);
132+
assert_eq!(antinodes, 34);
133+
}
134+
135+
#[test]
136+
fn test_part2() {
137+
let antinodes = read_graph(&get_input("input.txt")).find_antinodes(true);
138+
assert_eq!(antinodes, 1182);
139+
}
140+
162141
fn main() {
163-
read_graph(&get_input("prelim.txt")).find_antinodes();
164-
read_graph(&get_input("input.txt")).find_antinodes();
142+
read_graph(&get_input("prelim.txt")).find_antinodes(false);
143+
read_graph(&get_input("input.txt")).find_antinodes(false);
144+
read_graph(&get_input("prelim.txt")).find_antinodes(true);
145+
read_graph(&get_input("input.txt")).find_antinodes(true);
165146
}

0 commit comments

Comments
 (0)