Refactor code for API #3

Merged
mikael.capelle merged 13 commits from dev/refactor-for-ui into master 2024-12-08 13:06:42 +00:00
3 changed files with 133 additions and 2 deletions
Showing only changes of commit d1f4f5bed0 - Show all commits

View File

@ -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)
)
)
)
)

View File

@ -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.................

View File

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............