Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Dec 15, 2024
1 parent e4e2555 commit ee734d3
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
Empty file added day10/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions day10/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
676781023010121078756541010565410126589652103
787692014523134569687238921076321087676543012
896543210674013278798107831089980896567122107
654100134985329143237356540123678925498033498
783210325676438050123445443294541012321044589
694309018984567267034576356789032008769655678
345678567823432178125689219878102109678724369
456969430214563089068701008765210234569013212
327854321005478873879010123674377654354321001
218901232876569912968123294589988912210132432
107650343985567803451054387487676903432101561
210545674783498712589965236596567876543650170
323432185692105605678876145645430967858743289
214981092185434104987986001034321458969801001
105670123076393213098887632125010321578932102
789889874101284332106798540136521230432840013
876776965692379876087034567287650145521051224
965460150789561045696129898398010676670569345
234321041276432038765408765498723487989678496
165432132345987129932317454389654395432310987
074540122187656087801326761230101276211001236
783458043090345196540410890121210989303456545
892169834901210105432589789032367893456327896
701078985810012234501678776543456302587410187
667654856798943107657578905494543211693217896
578983012367874038748765412387687600784506787
457832343455465129889854307898990521099615690
300761567854321012934781212387121434988794321
211650434969482103245690101236012345670188760
672349123478091014132386789845621012873279454
589678012562182365001675632736790123964560303
432547001601276478976543541345887654456781212
321232118762345569885012310212994569323998800
210321129098710378894322343200123478010878901
300410030123601256765011056123430765430765432
321567542034510349810782987001521894321045645
434788601945654878725693965432676541013236012
095699717898783965434344876501987034901107823
187659826500192854303239884567698127872787934
234549834410201601214138793298012016543396543
067812765325360519871025682105623456671230123
154908901876451456789014871234702965580145674
233217010945962359874323960189811874495432985
945606321034876543265221054076320432356781876
876545432321089012100100123165410321065690165
70 changes: 70 additions & 0 deletions day10/part1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from __future__ import annotations

import argparse
import os.path

import pytest

import support

INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt')


def compute(s: str) -> int:
world = support.parse_coords_int(s)

def _score(pos: tuple[int, int]) -> int:
completed = set()
todo = [(pos, 0)]
while todo:
pos, size = todo.pop()

if size == 9:
completed.add(pos)
continue

for coord in support.adjacent_4(*pos):
if world.get(coord, -1) == size + 1:
todo.append((coord, size + 1))

return len(completed)

return sum(_score(k) for k, v in world.items() if v == 0)


INPUT_S = '''\
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
'''
EXPECTED = 36


@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())
70 changes: 70 additions & 0 deletions day10/part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from __future__ import annotations

import argparse
import os.path

import pytest

import support

INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt')


def compute(s: str) -> int:
world = support.parse_coords_int(s)

def _score(pos: tuple[int, int]) -> int:
completed = 0
todo = [(pos, 0)]
while todo:
pos, size = todo.pop()

if size == 9:
completed += 1
continue

for coord in support.adjacent_4(*pos):
if world.get(coord, -1) == size + 1:
todo.append((coord, size + 1))

return completed

return sum(_score(k) for k, v in world.items() if v == 0)


INPUT_S = '''\
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
'''
EXPECTED = 81


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

0 comments on commit ee734d3

Please sign in to comment.