Skip to content

Commit 5175b28

Browse files
committed
feat: upload day 8
1 parent cd19d06 commit 5175b28

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

2024/day-8/Cargo.lock

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2024/day-8/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day-8"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
glam = "0.29.2"
8+
itertools = "0.14.0"

2024/day-8/src/main.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// ╭─────────────────────────────────────────────────────────╮
2+
// │ Advent of Code 2024 - Day 8 │
3+
// ╰─────────────────────────────────────────────────────────╯
4+
5+
use std::collections::HashSet;
6+
7+
use glam::ivec2;
8+
9+
use itertools::Itertools;
10+
11+
fn main() {
12+
let input = include_str!("../input.txt");
13+
let frequencies = input
14+
.lines()
15+
.enumerate()
16+
.flat_map(|(y, line)| {
17+
line.chars()
18+
.enumerate()
19+
.filter(|(_, c)| *c != '.')
20+
.map(|(x, c)| (c, ivec2(x as i32, y as i32)))
21+
.collect::<Vec<_>>()
22+
})
23+
.into_group_map();
24+
let height = input.lines().count() as i32;
25+
let width = input.lines().next().unwrap().len() as i32;
26+
27+
// ── Part 1 ──────────────────────────────────────────────────────────
28+
let anti_nodes_count = frequencies
29+
.values()
30+
.flat_map(|antennas| {
31+
antennas.iter().flat_map(|first_pos| {
32+
antennas
33+
.iter()
34+
.filter(|&second_pos| first_pos != second_pos)
35+
.filter_map(|second_pos| {
36+
let diff = second_pos - first_pos;
37+
let anti_node = second_pos + diff;
38+
if anti_node.x >= 0
39+
&& anti_node.y >= 0
40+
&& anti_node.x < width
41+
&& anti_node.y < height
42+
{
43+
Some(anti_node)
44+
} else {
45+
None
46+
}
47+
})
48+
.collect::<Vec<_>>()
49+
})
50+
})
51+
.collect::<HashSet<_>>()
52+
.len();
53+
54+
println!("Antinodes count: {anti_nodes_count}");
55+
56+
// ── Part 2 ──────────────────────────────────────────────────────────
57+
let anti_nodes_count = frequencies
58+
.values()
59+
.flat_map(|antennas| {
60+
antennas.iter().flat_map(|first_pos| {
61+
antennas
62+
.iter()
63+
.filter(|&second_pos| first_pos != second_pos)
64+
.flat_map(|second_pos| {
65+
let diff = second_pos - first_pos;
66+
let mut anti_nodes = vec![];
67+
for i in 1.. {
68+
let anti_node = first_pos + diff * i;
69+
if anti_node.x >= 0
70+
&& anti_node.y >= 0
71+
&& anti_node.x < width
72+
&& anti_node.y < height
73+
{
74+
anti_nodes.push(anti_node)
75+
} else {
76+
break;
77+
}
78+
}
79+
anti_nodes
80+
})
81+
.collect::<Vec<_>>()
82+
})
83+
})
84+
.collect::<HashSet<_>>()
85+
.len();
86+
87+
println!("Antinodes count: {anti_nodes_count}");
88+
}

0 commit comments

Comments
 (0)