Skip to content

Commit edad63e

Browse files
committed
2024: Add solution for Day 8
1 parent 184805a commit edad63e

File tree

12 files changed

+279
-13
lines changed

12 files changed

+279
-13
lines changed

2024/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2024/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ members = [
2525
"day23",
2626
"day24",
2727
"day25",
28+
"lib",
2829
]
2930
exclude = []
3031
resolver = "2"

2024/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cargo run --bin day01
1717
5. [Print Queue](day05) 🌟🌟
1818
6. [Guard Gallivant](day06) 🌟🌟
1919
7. [Bridge Repair](day07) 🌟🌟
20-
8. [](day08)
20+
8. [Resonant Collinearity](day08) 🌟🌟
2121
9. [](day09)
2222
10. [](day10)
2323
11. [](day11)

2024/day08/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
lib = { path = "../lib" }

2024/day08/example1.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

2024/day08/input.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.....................U.........w..................
2+
l.................................................
3+
...........o.a................U...w...............
4+
............................................W.....
5+
..........T....................s.............7....
6+
.............................................W....
7+
.........T..............4....n.d.H.........5......
8+
......T.....oj...U.....n...w......H...........z...
9+
.G..x..........................E.....V..H.........
10+
.........a....................d....s.......7w.....
11+
...j....r.............o.............V.......d...W.
12+
.......r..J.Goa.U...............n................z
13+
.........Jj.........M..........Pv.................
14+
...J...........t..3..M..............sLV...........
15+
...................t................n.............
16+
....r...........X...........M........v............
17+
...x....t......I......a.PM...............W........
18+
...........1.Bj....I........vO.h.dL...............
19+
.........6....Rr......B...X........h..5v.L..z.....
20+
......1G...........x.....3B.......5...............
21+
.................B....0..........4..E.............
22+
.....................X.....5..h....P....f.....D...
23+
.......1........J.....eK..........................
24+
..................I....R....K...........k.........
25+
......G..................O........................
26+
...........H...9...............K8.P.4..k..E.......
27+
............1....3.............8.F.............f..
28+
.........................4........................
29+
.l...........X............9.......................
30+
....N.................R...t.e.....................
31+
...g............3..R.........e....h.........f.....
32+
...........................e......i...............
33+
................2...I.7..9..O.....s.........k.....
34+
....................6...9E.............F..O.......
35+
........................KN........................
36+
.......g......................Z.........F..f...Y..
37+
...........................A....i.................
38+
...........6g...b........8.......y.....S..........
39+
..l.....6.....m...............8...................
40+
....u..m...b...............p...A..................
41+
..............b.p........................k........
42+
....m......2...........Z..y....i..................
43+
........g2.....b.........i....D..ZF...............
44+
......2.0...........p............N..........A.....
45+
...m.............S...y........A...Z...N...........
46+
..S..l..........................................Y.
47+
........S....0u.................y......DY.........
48+
...........0.........................D............
49+
.................u...................p...Y........
50+
.......u..........................................

2024/day08/src/main.rs

Lines changed: 111 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
//! https://adventofcode.com/2024/day/8
33
44
use std::{fs, io};
5+
use std::collections::{BTreeMap, BTreeSet};
6+
use std::ops::Range;
57
use std::path::Path;
68

9+
use lib::vector::Vector;
10+
11+
type Vec2 = Vector<i32, 2>;
12+
713
fn main() {
814
let input = Input::from_file(format!("{}/input.txt", env!("CARGO_MANIFEST_DIR"))).expect("failed to read input");
915
//let input = Input::from_file(format!("{}/example1.txt", env!("CARGO_MANIFEST_DIR"))).expect("failed to read input");
10-
println!("{input:?}");
16+
//println!("{input:?}");
1117

1218
// Part 1
1319
println!("Part 1: {}", part1(&input));
@@ -17,24 +23,119 @@ fn main() {
1723
}
1824

1925
fn part1(input: &Input) -> usize {
20-
0
26+
let mut freqs: BTreeMap<_, Vec<_>> = BTreeMap::new();
27+
for (pos, c) in input.antennas.iter() {
28+
freqs.entry(*c).or_default().push(*pos);
29+
}
30+
31+
let mut antinodes = BTreeSet::new();
32+
for (_, positions) in freqs.into_iter() {
33+
for i in 0..positions.len() {
34+
for j in 0..positions.len() {
35+
if i == j {
36+
continue;
37+
}
38+
39+
let p1 = positions[i];
40+
let p2 = positions[j];
41+
42+
let d = p2 - p1;
43+
44+
let a1 = p2 + d;
45+
if in_bounds(&input.bounds, a1) {
46+
antinodes.insert(a1);
47+
}
48+
49+
let a2 = p1 - d;
50+
if in_bounds(&input.bounds, a2) {
51+
antinodes.insert(a2);
52+
}
53+
}
54+
}
55+
56+
}
57+
58+
antinodes.len()
2159
}
2260

2361
fn part2(input: &Input) -> usize {
24-
0
62+
let mut freqs: BTreeMap<_, Vec<_>> = BTreeMap::new();
63+
for (pos, c) in input.antennas.iter() {
64+
freqs.entry(*c).or_default().push(*pos);
65+
}
66+
67+
let mut antinodes = BTreeSet::new();
68+
for (_, positions) in freqs.into_iter() {
69+
for i in 0..positions.len() {
70+
for j in 0..positions.len() {
71+
if i == j {
72+
continue;
73+
}
74+
75+
let p1 = positions[i];
76+
let p2 = positions[j];
77+
78+
let d = p2 - p1;
79+
80+
let mut p = p2;
81+
loop {
82+
if !in_bounds(&input.bounds, p) {
83+
break;
84+
}
85+
86+
antinodes.insert(p);
87+
p += d;
88+
}
89+
90+
let mut p = p1;
91+
loop {
92+
if !in_bounds(&input.bounds, p) {
93+
break;
94+
}
95+
96+
antinodes.insert(p);
97+
p -= d;
98+
}
99+
}
100+
}
101+
}
102+
103+
antinodes.len()
104+
}
105+
106+
fn in_bounds(bounds: &(Range<i32>, Range<i32>), pos: Vec2) -> bool {
107+
bounds.0.contains(&pos[0]) && bounds.1.contains(&pos[1])
25108
}
26109

27110
#[derive(Debug, Clone)]
28111
struct Input {
29-
values: Vec<String>,
112+
bounds: (Range<i32>, Range<i32>),
113+
antennas: BTreeMap<Vec2, char>,
30114
}
31115

32116
impl Input {
33117
fn from_file(path: impl AsRef<Path>) -> io::Result<Self> {
34118
let input = fs::read_to_string(path)?;
35-
let values = input.lines().map(str::to_string).collect();
36119

37-
Ok(Self { values })
120+
let mut width = 0;
121+
let mut height = 0;
122+
let mut antennas = BTreeMap::new();
123+
for (y, line) in input.lines().enumerate() {
124+
for (x, c) in line.trim().chars().enumerate() {
125+
let pos = Vector::new([x as i32, y as i32]);
126+
127+
if c != '.' {
128+
antennas.insert(pos, c);
129+
}
130+
131+
width = x as i32 + 1;
132+
height = y as i32 + 1;
133+
}
134+
}
135+
136+
let bounds = (0..width, 0..height);
137+
138+
Ok(Self { bounds, antennas })
38139
}
39140
}
40141

@@ -46,27 +147,27 @@ mod test {
46147
fn test_part1() {
47148
let input = Input::from_file("example1.txt").unwrap();
48149

49-
assert_eq!(part1(&input), 0);
150+
assert_eq!(part1(&input), 14);
50151
}
51152

52153
#[test]
53154
fn test_part1_solution() {
54155
let input = Input::from_file("input.txt").unwrap();
55156

56-
assert_eq!(part1(&input), 0);
157+
assert_eq!(part1(&input), 423);
57158
}
58159

59160
#[test]
60161
fn test_part2() {
61162
let input = Input::from_file("example1.txt").unwrap();
62163

63-
assert_eq!(part2(&input), 0);
164+
assert_eq!(part2(&input), 34);
64165
}
65166

66167
#[test]
67168
fn test_part2_solution() {
68169
let input = Input::from_file("input.txt").unwrap();
69170

70-
assert_eq!(part2(&input), 0);
171+
assert_eq!(part2(&input), 1287);
71172
}
72173
}

2024/day25/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ name = "day25"
33
version = "0.1.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
6+
# see more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]

2024/lib/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "lib"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# see more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

2024/lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod vector;

0 commit comments

Comments
 (0)