-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |