Skip to content

Instantly share code, notes, and snippets.

@thrasibule
Created December 4, 2023 04:10
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 thrasibule/3cf456611b8f03546a1a6d456852954c to your computer and use it in GitHub Desktop.
Save thrasibule/3cf456611b8f03546a1a6d456852954c to your computer and use it in GitHub Desktop.
import sys
from itertools import product
with open(sys.argv[1], "rt") as fh:
digits = {}
specials = set()
for i, line in enumerate(fh):
line = line.rstrip()
begin = None
l = ""
for j, c in enumerate(line):
if c.isdigit():
l += c
if begin is None:
begin = j
else:
if begin is not None:
digits[int(l)] = (i, begin, j)
begin = None
l = ""
if c != ".":
specials.add((i, j))
res = 0
for k, (row, start, end) in digits.items():
for r, c in product([row-1, row, row+1], range(start-1, end+1)):
if (r, c) in specials:
res += k
break
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment