Skip to content

Commit

Permalink
2024 day 8 parts 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Dec 8, 2024
1 parent e7441d6 commit 78fb480
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★
- 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆
- 2023 - ★★★★★ ★★★★★ ★★★★★ ★★★☆
- 2024 - ★★★★★ ★★
- 2024 - ★★★★★ ★★

## How to use

Expand Down
39 changes: 39 additions & 0 deletions src/year2024/day08a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""2024 - Day 8 Part 1: Resonant Collinearity"""

from collections import defaultdict
from itertools import combinations


def solve(task: str) -> int:
data = task.split("\n")

rows = len(data)
cols = len(data[0])

antennas: dict[str, list[tuple[int, int]]] = defaultdict(list)

for r, row in enumerate(data):
for c, item in enumerate(row):
if item not in {".", "#"}:
antennas[item].append((r, c))

ants: set[tuple[int, int]] = set()

for k, v in antennas.items():
for a1, a2 in combinations(antennas[k], 2):
r1, c1 = a1
r2, c2 = a2

dr = r1 - r2
dc = c1 - c2

for ar, ac in (
(r1 - dr, c1 - dc),
(r1 + dr, c1 + dc),
(r2 - dr, c2 - dc),
(r2 + dr, c2 + dc),
):
if 0 <= ar < rows and 0 <= ac < cols and data[ar][ac] != k:
ants.add((ar, ac))

return len(ants)
62 changes: 62 additions & 0 deletions src/year2024/day08b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""2024 - Day 8 Part 2: Resonant Collinearity"""

from collections import defaultdict
from itertools import combinations


def solve(task: str) -> int:
data = task.split("\n")

rows = len(data)
cols = len(data[0])

antennas: dict[str, list[tuple[int, int]]] = defaultdict(list)

for r, row in enumerate(data):
for c, item in enumerate(row):
if item not in {".", "#"}:
antennas[item].append((r, c))

ants: set[tuple[int, int]] = set()

for k, v in antennas.items():
for a1, a2 in combinations(antennas[k], 2):
r1, c1 = a1
r2, c2 = a2

dr = r1 - r2
dc = c1 - c2

ar1 = r1 - dr
ac1 = c1 - dc

ar2 = r1 + dr
ac2 = c1 + dc

ar3 = r2 - dr
ac3 = c2 - dc

ar4 = r2 + dr
ac4 = c2 + dc

while 0 <= ar1 < rows and 0 <= ac1 < cols:
ants.add((ar1, ac1))
ar1 -= dr
ac1 -= dc

while 0 <= ar2 < rows and 0 <= ac2 < cols:
ants.add((ar2, ac2))
ar2 += dr
ac2 += dc

while 0 <= ar3 < rows and 0 <= ac3 < cols:
ants.add((ar3, ac3))
ar3 -= dr
ac3 -= dc

while 0 <= ar4 < rows and 0 <= ac4 < cols:
ants.add((ar4, ac4))
ar4 += dr
ac4 += dc

return len(ants)
25 changes: 25 additions & 0 deletions tests/src/year2024/test_day08a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""2024 - Day 8 Part 1: Resonant Collinearity"""

from textwrap import dedent

from src.year2024.day08a import solve


def test_solve():
task = dedent(
"""
......#....#
...#....0...
....#0....#.
..#....0....
....0....#..
.#....A.....
...#........
#......#....
........A...
.........A..
..........#.
..........#.
"""
).strip()
assert solve(task) == 14
25 changes: 25 additions & 0 deletions tests/src/year2024/test_day08b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""2024 - Day 8 Part 2: Resonant Collinearity"""

from textwrap import dedent

from src.year2024.day08b import solve


def test_solve():
task = dedent(
"""
##....#....#
.#.#....0...
..#.#0....#.
..##...0....
....0....#..
.#...#A....#
...#..#.....
#....#.#....
..#.....A...
....#....A..
.#........#.
...#......##
"""
).strip()
assert solve(task) == 34

0 comments on commit 78fb480

Please sign in to comment.