From d1f4f5bed0a1db04c1c65b7d2026815f64b58745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sun, 8 Dec 2024 08:53:36 +0100 Subject: [PATCH] 2024 day 8. --- src/holt59/aoc/2024/day8.py | 73 +++++++++++++++++++++- src/holt59/aoc/inputs/holt59/2024/day8.txt | 50 +++++++++++++++ src/holt59/aoc/inputs/tests/2024/day8.txt | 12 ++++ 3 files changed, 133 insertions(+), 2 deletions(-) diff --git a/src/holt59/aoc/2024/day8.py b/src/holt59/aoc/2024/day8.py index 07e201e..8c67a2d 100644 --- a/src/holt59/aoc/2024/day8.py +++ b/src/holt59/aoc/2024/day8.py @@ -1,7 +1,76 @@ -from typing import Any, Iterator +import itertools as it +from collections import defaultdict +from typing import Any, Iterator, cast from ..base import BaseSolver +def compute_antinodes( + a1: tuple[int, int], + a2: tuple[int, int], + n_rows: int, + n_cols: int, + min_distance: int = 1, + max_distance: int | None = 1, +): + if a1[0] > a2[0]: + a1, a2 = a2, a1 + + d_row, d_col = a2[0] - a1[0], a2[1] - a1[1] + + points: list[tuple[int, int]] = [] + + for c in range(min_distance, (max_distance or n_rows) + 1): + row_1, col_1 = a1[0] - c * d_row, a1[1] - c * d_col + row_2, col_2 = a2[0] + c * d_row, a2[1] + c * d_col + + valid_1, valid_2 = ( + 0 <= row_1 < n_rows and 0 <= col_1 < n_cols, + 0 <= row_2 < n_rows and 0 <= col_2 < n_cols, + ) + + if not valid_1 and not valid_2: + break + + if valid_1: + points.append((row_1, col_1)) + if valid_2: + points.append((row_2, col_2)) + + return tuple(points) + + class Solver(BaseSolver): - def solve(self, input: str) -> Iterator[Any]: ... + def solve(self, input: str) -> Iterator[Any]: + lines = input.splitlines() + + n_rows, n_cols = len(lines), len(lines[0]) + + antennas: dict[str, list[tuple[int, int]]] = defaultdict(list) + for i, j in it.product(range(n_rows), range(n_cols)): + if lines[i][j] != ".": + antennas[lines[i][j]].append((i, j)) + + yield len( + cast(set[tuple[int, int]], set()).union( + it.chain( + *( + compute_antinodes(a1, a2, n_rows, n_cols) + for antennas_of_frequency in antennas.values() + for a1, a2 in it.permutations(antennas_of_frequency, 2) + ) + ) + ) + ) + + yield len( + cast(set[tuple[int, int]], set()).union( + it.chain( + *( + compute_antinodes(a1, a2, n_rows, n_cols, 0, None) + for antennas_of_frequency in antennas.values() + for a1, a2 in it.permutations(antennas_of_frequency, 2) + ) + ) + ) + ) diff --git a/src/holt59/aoc/inputs/holt59/2024/day8.txt b/src/holt59/aoc/inputs/holt59/2024/day8.txt index e69de29..1e7f2b1 100644 --- a/src/holt59/aoc/inputs/holt59/2024/day8.txt +++ b/src/holt59/aoc/inputs/holt59/2024/day8.txt @@ -0,0 +1,50 @@ +.........................p........................ +......................h....C............M......... +..............................p....U.............. +..5..................p............................ +..6z...........................................C.. +...............c...........zV..................... +...5.....c........................................ +.Z.............h........S...z....9................ +.O............................9...z........M..C... +..O....5..............................F..M..C..... +..Z.........5.c...............M....V.............. +........ZO................q....................... +s...O................h..Uq.....7V...........4..... +.q.g..............c.............p.......4......... +............hZ.............................4G..... +6s...........................U.Q.....3............ +.......6.................9.......Q.............3.. +....s..D.........................6................ +.............................................FL... +.................................................. +..g...D.........q.....f.......Q...F....L......7... +...............2.........f.............V.L...4.... +...................2.s...................f3......G +....g...........................v......7P......... +..2..g.............d.....v...........P.......1.... +..............u.........f.............L........G.. +.........l.D....u...............d........o..P..... +..................8...............9..1......o...7. +............l..................................... +...................l...S...........F.......o..U... +.......................u...S...................... +..........l....u...............m...........P....G. +......................................1.8.......o. +.................................................. +..................v.......S................0...... +.............v........d.....1..................... +.................................................. +..........D....................................0.. +...................m.............H..........0..... +...................................d......0....... +.................................................. +....Q............................................. +................................H................. +........................H....................8.... +.................................................. +.................................................. +.........................................8........ +.......................H3......................... +............................m..................... +................................m................. diff --git a/src/holt59/aoc/inputs/tests/2024/day8.txt b/src/holt59/aoc/inputs/tests/2024/day8.txt index e69de29..78a1e91 100644 --- a/src/holt59/aoc/inputs/tests/2024/day8.txt +++ b/src/holt59/aoc/inputs/tests/2024/day8.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............