Skip to content

Commit 051d0e3

Browse files
committed
Add 2024 day08 solution
1 parent b18273e commit 051d0e3

File tree

6 files changed

+245
-0
lines changed

6 files changed

+245
-0
lines changed

2024/day08/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Day 08 - Resonant Collinearity
2+
3+
> [!NOTE]
4+
> Input link: <https://adventofcode.com/2024/day/8/input>
5+
6+
## Part 1
7+
8+
> [!NOTE]
9+
> Problem link: <https://adventofcode.com/2024/day/8>
10+
11+
### Part 1 Solution
12+
13+
```bash
14+
$ cargo run --release -- --input 2024/day08/input.txt 2024 08 part1
15+
295
16+
```
17+
18+
## Part 2
19+
20+
> [!NOTE]
21+
> Problem link: <https://adventofcode.com/2024/day/8#part2>
22+
23+
### Part 2 Solution
24+
25+
```bash
26+
$ cargo run --release -- --input 2024/day08/input.txt 2024 08 part2
27+
1034
28+
```

2024/day08/input.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
..f........................8......................
2+
G............8..u.................................
3+
........G...p.....................................
4+
......d.....................n.....................
5+
..................................................
6+
.....................................K............
7+
..................F...8.n...B.K...................
8+
....b.........8..u................................
9+
...............F.p.......B.............4.....5....
10+
....f..d..U.........................c.............
11+
...........U.....d.n.u.0................5.........
12+
...Y.......f..........................5...........
13+
..........................u.....d.....e.........4.
14+
..F...p.............v........n....................
15+
....s.............0...............................
16+
...US.s....g.....D..........................4.....
17+
......wG...............S..........................
18+
.............................B.....e..............
19+
.........w.........................A..............
20+
.............9w.g..........................4......
21+
U....9..g.....v.....P....D.....f.K................
22+
.s.............0..9........pP..........5..........
23+
..9s...................P..........................
24+
.............b..................0.....A..2....e...
25+
....................b.....V..v....................
26+
.......7........B......................A..........
27+
..................D6...V....q.....................
28+
...v............D....PV...........................
29+
.........Y...........g.......................e..y.
30+
.......SW......V..7....................c..........
31+
.......bY7.....................N........A.........
32+
.....................q.N..........y...............
33+
........................N.........c...............
34+
..................................................
35+
.........C..7..................q........2.........
36+
............................N...q.................
37+
...W......C3...Q................a1.........y......
38+
.......W.......................3......2...........
39+
........3...........6.............1...............
40+
....3............C.1....................k.........
41+
E..................................a.....c........
42+
.............................................w....
43+
..S.......................Q..........2......k.....
44+
......................C....6.......Q......ak......
45+
..................................................
46+
.................E.............a1............y....
47+
W..........E......................................
48+
......E...........6...........Q...................
49+
...........................k......................
50+
..................................................

2024/day08/input_example.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+
............

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ This repository hosts my solutions (and sometimes attempts) for
2020
- Day 05: [Print Queue](./2024/day05/README.md)
2121
- Day 06: [Guard Gallivant](./2024/day06/README.md)
2222
- Day 07: [Bridge Repair](./2024/day07/README.md)
23+
- Day 08: [Resonant Collinearity](./2024/day08/README.md)

src/year2024/day08.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
use std::collections::{HashMap, HashSet};
2+
3+
use anyhow::Result;
4+
use tracing::info;
5+
6+
#[derive(Debug, clap::Args)]
7+
pub struct Args {
8+
#[command(subcommand)]
9+
pub command: Commands,
10+
}
11+
12+
#[derive(Debug, clap::Subcommand)]
13+
pub enum Commands {
14+
/// Part 1.
15+
Part1,
16+
17+
/// Part 2.
18+
Part2,
19+
}
20+
21+
impl Args {
22+
pub fn run(self, input: String) -> Result<()> {
23+
match self.command {
24+
Commands::Part1 => part1(input),
25+
Commands::Part2 => part2(input),
26+
}
27+
}
28+
}
29+
30+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31+
struct Position {
32+
x: isize,
33+
y: isize,
34+
}
35+
36+
fn part1(input: String) -> Result<()> {
37+
let width = input.split_once('\n').unwrap().0.len().try_into().unwrap();
38+
let height = input.lines().count().try_into().unwrap();
39+
info!("width:{width} height:{height}");
40+
41+
let antennas: HashMap<String, Vec<Position>> = input
42+
.lines()
43+
.enumerate()
44+
.flat_map(|(y, l)| {
45+
l.chars().enumerate().filter(|(_, c)| *c != '.').map(move |(x, c)| {
46+
(
47+
c.to_string(),
48+
Position {
49+
x: x.try_into().unwrap(),
50+
y: y.try_into().unwrap(),
51+
},
52+
)
53+
})
54+
})
55+
.fold(HashMap::new(), |mut acc, (s, p)| {
56+
acc.entry(s).or_default().push(p);
57+
acc
58+
});
59+
info!("antennas: {antennas:?}");
60+
61+
let positions: HashSet<Position> = antennas.values().flatten().cloned().collect();
62+
info!("positions: {positions:?}");
63+
64+
let mut antinodes = HashSet::new();
65+
for positions in antennas.values().filter(|p| p.len() > 1) {
66+
for i in 0..positions.len() {
67+
let left = &positions[i];
68+
for right in positions[0..i].iter().chain(&positions[i + 1..]) {
69+
info!("left:{left:?} right:{right:?}");
70+
let vector = (right.x - left.x, right.y - left.y);
71+
let new_pos = Position {
72+
x: right.x + vector.0,
73+
y: right.y + vector.1,
74+
};
75+
if new_pos.x < 0 || new_pos.x >= width {
76+
continue;
77+
}
78+
if new_pos.y < 0 || new_pos.y >= height {
79+
continue;
80+
}
81+
antinodes.insert(new_pos);
82+
}
83+
}
84+
}
85+
info!("antinodes: {antinodes:?}");
86+
87+
println!("{}", antinodes.len());
88+
Ok(())
89+
}
90+
91+
fn part2(input: String) -> Result<()> {
92+
let width = input.split_once('\n').unwrap().0.len().try_into().unwrap();
93+
let height = input.lines().count().try_into().unwrap();
94+
info!("width:{width} height:{height}");
95+
96+
let antennas: HashMap<String, Vec<Position>> = input
97+
.lines()
98+
.enumerate()
99+
.flat_map(|(y, l)| {
100+
l.chars().enumerate().filter(|(_, c)| *c != '.').map(move |(x, c)| {
101+
(
102+
c.to_string(),
103+
Position {
104+
x: x.try_into().unwrap(),
105+
y: y.try_into().unwrap(),
106+
},
107+
)
108+
})
109+
})
110+
.fold(HashMap::new(), |mut acc, (s, p)| {
111+
acc.entry(s).or_default().push(p);
112+
acc
113+
});
114+
info!("antennas: {antennas:?}");
115+
116+
let positions: HashSet<Position> = antennas.values().flatten().cloned().collect();
117+
info!("positions: {positions:?}");
118+
119+
let mut antinodes = HashSet::new();
120+
for positions in antennas.values().filter(|p| p.len() > 1) {
121+
for pos_i in 0..positions.len() {
122+
let left = &positions[pos_i];
123+
'positions: for right in positions[0..pos_i].iter().chain(&positions[pos_i + 1..]) {
124+
antinodes.insert(Position { x: left.x, y: left.y });
125+
antinodes.insert(Position { x: right.x, y: right.y });
126+
127+
info!("left:{left:?} right:{right:?}");
128+
let vector = (right.x - left.x, right.y - left.y);
129+
for i in 1.. {
130+
let new_pos = Position {
131+
x: right.x + vector.0 * i,
132+
y: right.y + vector.1 * i,
133+
};
134+
if new_pos.x < 0 || new_pos.x >= width {
135+
continue 'positions;
136+
}
137+
if new_pos.y < 0 || new_pos.y >= height {
138+
continue 'positions;
139+
}
140+
antinodes.insert(new_pos);
141+
}
142+
}
143+
}
144+
}
145+
info!("antinodes: {antinodes:?}");
146+
147+
println!("{}", antinodes.len());
148+
Ok(())
149+
}

src/year2024/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod day04;
55
mod day05;
66
mod day06;
77
mod day07;
8+
mod day08;
89

910
use anyhow::Result;
1011

@@ -37,6 +38,9 @@ pub enum Commands {
3738
/// Advent of Code 2024 - Day 07 - Bridge Repair.
3839
#[command(visible_aliases = &["day7", "07", "7"])]
3940
Day07(day07::Args),
41+
/// Advent of Code 2024 - Day 08 - Resonant Collinearity.
42+
#[command(visible_aliases = &["day8", "08", "8"])]
43+
Day08(day08::Args),
4044
}
4145

4246
impl Args {
@@ -49,6 +53,7 @@ impl Args {
4953
Commands::Day05(args) => args.run(input),
5054
Commands::Day06(args) => args.run(input),
5155
Commands::Day07(args) => args.run(input),
56+
Commands::Day08(args) => args.run(input),
5257
}
5358
}
5459
}

0 commit comments

Comments
 (0)