Skip to content

Instantly share code, notes, and snippets.

@sciyoshi
Created December 9, 2021 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sciyoshi/649c78516f9d2ea7c118f6d7df950d74 to your computer and use it in GitHub Desktop.
Save sciyoshi/649c78516f9d2ea7c118f6d7df950d74 to your computer and use it in GitHub Desktop.
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