Skip to content

Commit 116d01e

Browse files
committed
2024: day 8. part 1. python
1 parent 1a7931c commit 116d01e

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/2024/day08/1/data.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
...............e.........................i........
2+
...............................1.......i.0........
3+
..............................s.0......d..........
4+
........................i....B.I............d.....
5+
.............................s....................
6+
................J.................................
7+
.....................L.....0i.......4...d.........
8+
.N...e...........................s..R.....4.....I.
9+
........e.........v................1......R....I..
10+
.............G..............0.....1...............
11+
..2...N.............B......................4...R..
12+
..............2...................N..........4s...
13+
..p...................................1..b..I.....
14+
..................p...........B........b...R......
15+
....................................b.............
16+
........W.......C.....w...........................
17+
............7....u.............B..................
18+
...W.................u......................bw....
19+
.......p.2...........v......................9.....
20+
.E.....C....u................................9....
21+
E....Y................u.D........9...........J....
22+
.......2..........................................
23+
............................J.................c...
24+
.............7...K..D..............J..............
25+
.....C.Wq........t.................T..............
26+
............Yt......v.............................
27+
..W......................3...............w........
28+
..7.....j................T...D.....n......8.....c.
29+
.........E...............nTD......................
30+
...r....E..........Y............n.......P........c
31+
......K........G......L...........................
32+
......................G.....L....v................
33+
..............G...t......q.............l.8........
34+
......................q............l..............
35+
...6........r.............................w..c....
36+
..6.........3.......Qk........T...................
37+
......Y...............j.................n.........
38+
..K.....S.....r......j.....U......9.l......8......
39+
........................U......................P..
40+
.....................q............................
41+
.......K......5..N.....j.7.Q......................
42+
...................p..k...U..........L.Q..........
43+
.r......3...S.......k........y....8U....Q.......P.
44+
.......S....g..3..................................
45+
.....S..........gk................................
46+
................5...................yP............
47+
.......................g......yV..l...............
48+
.........6.5...............V......................
49+
..................6..5..V.........................
50+
.............g.......................y..........V.

src/2024/day08/1/solution.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
from itertools import product
3+
4+
5+
def read_lines() -> list[str]:
6+
filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data.txt')
7+
with open(filename, "rb") as f:
8+
return [line.decode("utf-8").strip() for line in f.readlines()]
9+
10+
11+
lines = read_lines()
12+
matrix = [list(line) for line in lines]
13+
14+
15+
def is_within_bounds(point, bounds):
16+
return 0 <= point[0] < bounds[0] and 0 <= point[1] < bounds[1]
17+
18+
19+
def find_antinodes(matrix):
20+
height, width = len(matrix), len(matrix[0])
21+
antennas = {}
22+
23+
# antennas by frequency
24+
for y, x in product(range(height), range(width)):
25+
if matrix[y][x] != '.':
26+
antennas.setdefault(matrix[y][x], []).append((x, y))
27+
28+
antinodes = set()
29+
30+
for _, antenna_list in antennas.items():
31+
for i, (x1, y1) in enumerate(antenna_list):
32+
for (x2, y2) in antenna_list[i + 1:]:
33+
x_diff, y_diff = x2 - x1, y2 - y1
34+
35+
# calculate symmetrical antinodes
36+
outside_a = (x1 - x_diff, y1 - y_diff)
37+
outside_b = (x2 + x_diff, y2 + y_diff)
38+
39+
if is_within_bounds(outside_a, (width, height)):
40+
antinodes.add(outside_a)
41+
if is_within_bounds(outside_b, (width, height)):
42+
antinodes.add(outside_b)
43+
44+
return len(antinodes)
45+
46+
47+
result = find_antinodes(matrix)
48+
print(result)

0 commit comments

Comments
 (0)