Skip to content

Commit b3b41c1

Browse files
committed
2024: Day 8
1 parent ed97b8d commit b3b41c1

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

2024/python/day8/__init__.py

Whitespace-only changes.

2024/python/day8/__main__.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import math
2+
from itertools import combinations
3+
4+
from black.trans import defaultdict
5+
6+
from utils.file import read_input
7+
8+
lines = read_input(__package__)
9+
10+
height = len(lines)
11+
width = len(lines[0])
12+
13+
expected: set[complex] = set()
14+
all_antennas: defaultdict[str, set[complex]] = defaultdict(set)
15+
for y, line in enumerate(lines):
16+
for x, c in enumerate(line):
17+
if c not in {".", "#"}:
18+
all_antennas[c].add(complex(x, y))
19+
expected.add(complex(x, y))
20+
if c == "#":
21+
expected.add(complex(x, y))
22+
23+
antinodes: set[complex] = set()
24+
for frequency, antennas in all_antennas.items():
25+
for a, b in combinations(antennas, 2):
26+
diff = b - a
27+
antinodes.add(a - diff)
28+
antinodes.add(b + diff)
29+
30+
valid_antinodes = {a for a in antinodes if 0 <= a.real < width and 0 <= a.imag < height}
31+
print("Part 1:", len(valid_antinodes))
32+
33+
antinodes_p2 = valid_antinodes.copy() | {a for antennas in all_antennas.values() for a in antennas}
34+
for frequency, antennas in all_antennas.items():
35+
for a, b in combinations(antennas, 2):
36+
diff = b - a
37+
candidates = set()
38+
for i in range(1, min(width, height)):
39+
candidates.add(a - i * diff)
40+
candidates.add(b + i * diff)
41+
for candidate in candidates:
42+
if 0 <= candidate.real < width and 0 <= candidate.imag < height:
43+
antinodes_p2.add(candidate)
44+
45+
print("Part 2:", len(antinodes_p2))

2024/python/day8/input.txt

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
...........g......................................
2+
......e.g......s.............R...........I........
3+
............g........................I............
4+
..b......Q....s.....................P.............
5+
.......e..T...................K...........P...F...
6+
.....g................U.............4.............
7+
.........b..........4..RU..................1.F....
8+
....a.....Q..........b........R..U...............1
9+
.S...T............s.............I.........f.......
10+
...A....T...............................I.........
11+
.....Qa............A.G...K...........P............
12+
...........................G................1.....
13+
...D...................................4.f........
14+
..................................................
15+
................k.......R....................t....
16+
.........T.e..............K........u.......t......
17+
....................A.............................
18+
......S....a.............F...........KG...........
19+
....D...h......k..................................
20+
..D...............k............................4..
21+
..............................................i...
22+
.........S..................d.....................
23+
......QU....S......s..d...G............i..........
24+
...........d....9...F.h...E.......................
25+
.................d....B...........................
26+
...h........................H.......t.............
27+
.........h.B..3.E.............H..r................
28+
.......E......B......2...5.....H..................
29+
.z...........9................................t...
30+
.....9...D........................................
31+
.....Z.......39..a................................
32+
..........3.............r.........................
33+
...............Er.............................7...
34+
.........................J...k.r.q.......i.8.p....
35+
.......................u...............H.p..q.....
36+
..............................i.u6........p.......
37+
....................................0.............
38+
...............3..J....P...0......................
39+
........................2j........................
40+
...............................j2B0...............
41+
................J..2...5.....6......p....8........
42+
............y.........................7...........
43+
..............5.........y...........6.............
44+
................................j.................
45+
.........................Y.J.....0................
46+
.........................................y........
47+
..................Z...uy...................q......
48+
.......z.........Z............Y.6.............8...
49+
z.........................Y..........7............
50+
....................Z...5..Y......................

0 commit comments

Comments
 (0)