Skip to content

Commit 11b2a0f

Browse files
committed
Add day 8
1 parent edeb213 commit 11b2a0f

File tree

7 files changed

+227
-7
lines changed

7 files changed

+227
-7
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ edition = "2021"
66
[dependencies]
77
aoc-runner = "0.3.0"
88
aoc-runner-derive = "0.3.0"
9+
itertools = "0.13.0"
910
memoize = "0.4.2"
1011
regex = "1.11.1"

input/2024/day8.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
...........................A.............W........
2+
.......x..........AZ......P.........m......k..W...
3+
....v.....Z..K...V..........A.......R....f........
4+
.............d..........V.............2...........
5+
.........Z........d...V.....B....C.....e..........
6+
............Z.vA...........m...................s.W
7+
...x..o.....................ek....................
8+
...O.....VK..................R......B.............
9+
.........O..................my....kB..............
10+
.v...................y..........C........B.....z..
11+
xb.v..................................C..z........
12+
.........................2.ey.....................
13+
..K.......................y...................s...
14+
..........................................z.......
15+
.......................2.......R........z......F..
16+
O.....d..................D..k.........F...........
17+
..........O..........D.............s....E.........
18+
..o......9....................D........C..s.......
19+
.....o..r....................c...................m
20+
.......P..............................e...........
21+
..............1......................d............
22+
...................o...............3..............
23+
........................c.......24..........S.....
24+
....x..................c.............F4.........S.
25+
P............N..8.......W.D.......................
26+
.....K............1.8.............................
27+
.........P..............Q...M.....................
28+
9...................R........4...8.....0..........
29+
....n.........................F...................
30+
........Y...n......1......................3..J....
31+
.........................3...8....................
32+
...n...M............................0.......ja...S
33+
....f................................6.....S.....E
34+
....................i.........M...J...............
35+
...r..........q..........1......l..0.....L........
36+
.........f7....i.Nc......................j.......l
37+
..9....Y7......X.........Q...M5....J4...........3.
38+
.Y........NX..I............Q........L.............
39+
......Xw...nb.............0..............l6.......
40+
......b.....f....5..q.....................a..6....
41+
.......5..........iq.9.....p..........a...........
42+
........X........I................p...6...........
43+
..................N.............L.........j.......
44+
...b.7.......................p....Q......E........
45+
....Y....7......................p................j
46+
.......r..........................................
47+
.................i...................a............
48+
..w.........5.....................................
49+
......w........I..............J...................
50+
.w............r.....................lL............

src/day12.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// use aoc_runner_derive::{aoc, aoc_generator};
2+
3+
// #[aoc_generator(day8)]
4+
// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) {
5+
6+
// #[aoc(day8, part1)]
7+
// pub fn part1(input: &[Equation]) -> usize {
8+
9+
// #[aoc(day8, part2)]
10+
// pub fn part2(input: &[Equation]) -> usize {

src/day8.rs

Lines changed: 139 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,142 @@
1-
// use aoc_runner_derive::{aoc, aoc_generator};
1+
use aoc_runner_derive::{aoc, aoc_generator};
2+
use std::collections::{HashMap, HashSet};
23

3-
// #[aoc_generator(day8)]
4-
// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) {
4+
#[derive(Debug)]
5+
pub struct City {
6+
frequencies: HashMap<char, HashSet<(usize, usize)>>,
7+
y_len: usize,
8+
x_len: usize,
9+
}
510

6-
// #[aoc(day8, part1)]
7-
// pub fn part1(input: &[Equation]) -> usize {
11+
#[aoc_generator(day8)]
12+
pub fn input_generator(input: &str) -> City {
13+
let mut frequencies: HashMap<char, HashSet<(usize, usize)>> = HashMap::new();
14+
let map: Vec<Vec<char>> = input
15+
.lines()
16+
.enumerate()
17+
.map(|(y, l)| {
18+
l.chars()
19+
.enumerate()
20+
.map(|(x, c)| {
21+
if c != '.' {
22+
frequencies.entry(c).or_default().insert((y, x));
23+
}
24+
c
25+
})
26+
.collect()
27+
})
28+
.collect();
29+
City {
30+
y_len: map.len(),
31+
x_len: map[0].len(),
32+
frequencies,
33+
}
34+
}
835

9-
// #[aoc(day8, part2)]
10-
// pub fn part2(input: &[Equation]) -> usize {
36+
#[aoc(day8, part1)]
37+
pub fn part1(input: &City) -> usize {
38+
let mut antinodes: HashSet<(usize, usize)> = HashSet::new();
39+
let mut count = 0;
40+
for positions in input.frequencies.values() {
41+
for current_pos in positions {
42+
for around_pos in positions {
43+
if current_pos == around_pos {
44+
continue;
45+
}
46+
let y = (current_pos.0 * 2).wrapping_sub(around_pos.0);
47+
let x = (current_pos.1 * 2).wrapping_sub(around_pos.1);
48+
if y < input.y_len && x < input.x_len && !antinodes.contains(&(y, x)) {
49+
count += 1;
50+
antinodes.insert((y, x));
51+
}
52+
}
53+
}
54+
}
55+
count
56+
}
57+
58+
#[aoc(day8, part2)]
59+
pub fn part2(input: &City) -> usize {
60+
let mut antinodes: HashSet<(usize, usize)> = HashSet::new();
61+
let mut count = 0;
62+
for positions in input.frequencies.values() {
63+
for current_pos in positions {
64+
for around_pos in positions {
65+
if current_pos == around_pos {
66+
continue;
67+
}
68+
let delta_y: isize = current_pos.0 as isize - around_pos.0 as isize;
69+
let delta_x: isize = current_pos.1 as isize - around_pos.1 as isize;
70+
71+
let mut y: isize = around_pos.0 as isize;
72+
let mut x: isize = around_pos.1 as isize;
73+
loop {
74+
y -= delta_y;
75+
x -= delta_x;
76+
if x < 0 || y < 0 || x >= input.x_len as isize || y >= input.y_len as isize {
77+
break;
78+
}
79+
if !antinodes.contains(&(y as usize, x as usize)) {
80+
count += 1;
81+
antinodes.insert((y as usize, x as usize));
82+
}
83+
}
84+
85+
y = around_pos.0 as isize;
86+
x = around_pos.1 as isize;
87+
loop {
88+
y = y.wrapping_add(delta_y);
89+
x = x.wrapping_add(delta_x);
90+
if x < 0 || y < 0 || x >= input.x_len as isize || y >= input.y_len as isize {
91+
break;
92+
}
93+
if !antinodes.contains(&(y as usize, x as usize)) {
94+
count += 1;
95+
antinodes.insert((y as usize, x as usize));
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
count
103+
}
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use super::*;
108+
109+
const INPUT: &str = "............
110+
........0...
111+
.....0......
112+
.......0....
113+
....0.......
114+
......A.....
115+
............
116+
............
117+
........A...
118+
.........A..
119+
............
120+
............
121+
";
122+
// const INPUT: &str = "T.........
123+
// ...T......
124+
// .T........
125+
// ..........
126+
// ..........
127+
// ..........
128+
// ..........
129+
// ..........
130+
// ..........
131+
// ..........";
132+
133+
#[test]
134+
fn test_part1() {
135+
assert_eq!(14, part1(&input_generator(INPUT)))
136+
}
137+
138+
#[test]
139+
fn test_part2() {
140+
assert_eq!(34, part2(&input_generator(INPUT)))
141+
}
142+
}

src/day9.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// use aoc_runner_derive::{aoc, aoc_generator};
2+
3+
// #[aoc_generator(day8)]
4+
// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) {
5+
6+
// #[aoc(day8, part1)]
7+
// pub fn part1(input: &[Equation]) -> usize {
8+
9+
// #[aoc(day8, part2)]
10+
// pub fn part2(input: &[Equation]) -> usize {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ mod day4;
99
mod day5;
1010
mod day6;
1111
mod day7;
12+
mod day8;
1213

1314
aoc_lib! { year = 2024 }

0 commit comments

Comments
 (0)