Created
December 9, 2021 05:53
-
-
Save sciyoshi/649c78516f9d2ea7c118f6d7df950d74 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from utils import Pt, Grd | |
import math | |
grd = Grd() | |
for i, l in enumerate(LINES): | |
for j, c in enumerate(l): | |
grd[Pt(i, j)] = int(c) | |
def expand(pt): | |
basin = {pt} | |
queue = [pt] | |
while queue: | |
p = queue.pop() | |
for nb in p.nb4: | |
if nb not in basin and nb in grd and grd[nb] < 9 and grd[nb] > grd[p]: | |
basin.add(nb) | |
queue.append(nb) | |
return basin | |
tot = 0 | |
basins = [] | |
for pt in grd: | |
if all(n not in grd or grd[pt] < grd[n] for n in pt.nb4): | |
tot += grd[pt] + 1 | |
basins.append(len(expand(pt))) | |
print("part 1", tot) | |
print("part 2", math.prod(list(sorted(basins))[-3:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment