This repository has been archived by the owner on Dec 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
4 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,50 @@ | ||
...........g...................................... | ||
......e.g......s.............R...........I........ | ||
............g........................I............ | ||
..b......Q....s.....................P............. | ||
.......e..T...................K...........P...F... | ||
.....g................U.............4............. | ||
.........b..........4..RU..................1.F.... | ||
....a.....Q..........b........R..U...............1 | ||
.S...T............s.............I.........f....... | ||
...A....T...............................I......... | ||
.....Qa............A.G...K...........P............ | ||
...........................G................1..... | ||
...D...................................4.f........ | ||
.................................................. | ||
................k.......R....................t.... | ||
.........T.e..............K........u.......t...... | ||
....................A............................. | ||
......S....a.............F...........KG........... | ||
....D...h......k.................................. | ||
..D...............k............................4.. | ||
..............................................i... | ||
.........S..................d..................... | ||
......QU....S......s..d...G............i.......... | ||
...........d....9...F.h...E....................... | ||
.................d....B........................... | ||
...h........................H.......t............. | ||
.........h.B..3.E.............H..r................ | ||
.......E......B......2...5.....H.................. | ||
.z...........9................................t... | ||
.....9...D........................................ | ||
.....Z.......39..a................................ | ||
..........3.............r......................... | ||
...............Er.............................7... | ||
.........................J...k.r.q.......i.8.p.... | ||
.......................u...............H.p..q..... | ||
..............................i.u6........p....... | ||
....................................0............. | ||
...............3..J....P...0...................... | ||
........................2j........................ | ||
...............................j2B0............... | ||
................J..2...5.....6......p....8........ | ||
............y.........................7........... | ||
..............5.........y...........6............. | ||
................................j................. | ||
.........................Y.J.....0................ | ||
.........................................y........ | ||
..................Z...uy...................q...... | ||
.......z.........Z............Y.6.............8... | ||
z.........................Y..........7............ | ||
....................Z...5..Y...................... |
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,76 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import collections | ||
import itertools | ||
import os.path | ||
|
||
import pytest | ||
|
||
import support | ||
|
||
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt') | ||
|
||
|
||
def compute(s: str) -> int: | ||
beacons: dict[str, list[tuple[int, int]]] | ||
beacons = collections.defaultdict(list) | ||
|
||
for y, line in enumerate(s.splitlines()): | ||
for x, c in enumerate(line): | ||
if c != '.': | ||
beacons[c].append((x, y)) | ||
|
||
bx = range(0, len(s.splitlines()[0])) | ||
by = range(0, len(s.splitlines())) | ||
|
||
antinodes = set() | ||
for nodes in beacons.values(): | ||
for (x1, y1), (x2, y2) in itertools.combinations(nodes, 2): | ||
dx, dy = x2 - x1, y2 - y1 | ||
antinodes.add((x2 + dx, y2 + dy)) | ||
antinodes.add((x1 - dx, y1 - dy)) | ||
|
||
return len({(x, y) for x, y in antinodes if x in bx if y in by}) | ||
|
||
|
||
INPUT_S = '''\ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ | ||
''' | ||
EXPECTED = 14 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('input_s', 'expected'), | ||
( | ||
(INPUT_S, EXPECTED), | ||
), | ||
) | ||
def test(input_s: str, expected: int) -> None: | ||
assert compute(input_s) == expected | ||
|
||
|
||
def main() -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('data_file', nargs='?', default=INPUT_TXT) | ||
args = parser.parse_args() | ||
|
||
with open(args.data_file) as f, support.timing(): | ||
print(compute(f.read())) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
raise SystemExit(main()) |
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,86 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import collections | ||
import itertools | ||
import os.path | ||
|
||
import pytest | ||
|
||
import support | ||
|
||
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt') | ||
|
||
|
||
def compute(s: str) -> int: | ||
beacons: dict[str, list[tuple[int, int]]] | ||
beacons = collections.defaultdict(list) | ||
|
||
for y, line in enumerate(s.splitlines()): | ||
for x, c in enumerate(line): | ||
if c != '.': | ||
beacons[c].append((x, y)) | ||
|
||
bx = range(0, len(s.splitlines()[0])) | ||
by = range(0, len(s.splitlines())) | ||
|
||
antinodes = set() | ||
for nodes in beacons.values(): | ||
for (x1, y1), (x2, y2) in itertools.combinations(nodes, 2): | ||
dx, dy = x2 - x1, y2 - y1 | ||
|
||
# positive | ||
cx, cy = x2, y2 | ||
while cx in bx and cy in by: | ||
antinodes.add((cx, cy)) | ||
cx, cy = cx + dx, cy + dy | ||
|
||
# negative | ||
cx, cy = x1, y1 | ||
while cx in bx and cy in by: | ||
antinodes.add((cx, cy)) | ||
cx, cy = cx - dx, cy - dy | ||
|
||
return len(antinodes) | ||
|
||
|
||
INPUT_S = '''\ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ | ||
''' | ||
EXPECTED = 34 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('input_s', 'expected'), | ||
( | ||
(INPUT_S, EXPECTED), | ||
), | ||
) | ||
def test(input_s: str, expected: int) -> None: | ||
assert compute(input_s) == expected | ||
|
||
|
||
def main() -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('data_file', nargs='?', default=INPUT_TXT) | ||
args = parser.parse_args() | ||
|
||
with open(args.data_file) as f, support.timing(): | ||
print(compute(f.read())) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == '__main__': | ||
raise SystemExit(main()) |