43 lines
1 KiB
Python
43 lines
1 KiB
Python
input = open("input", 'r')
|
|
|
|
# d = [line for line in input]
|
|
# d = [[val for val in line.strip()] for line in input]
|
|
d = {(complex(x,y)): int(c) for y,line in enumerate(input) for x,c in enumerate(line.strip())}
|
|
|
|
z = [p for p,c in d.items() if c == 0]
|
|
|
|
# print(d, z)
|
|
|
|
def run(p2=False):
|
|
scores = []
|
|
ratings = []
|
|
for zero in z:
|
|
score = 0
|
|
q = []
|
|
s = set()
|
|
nines = set()
|
|
nines2 = 0
|
|
|
|
q.append(zero)
|
|
|
|
while q:
|
|
point = q.pop()
|
|
if d[point] == 9:
|
|
nines.add(point)
|
|
nines2 += 1
|
|
continue
|
|
if not p2 and point in s:
|
|
continue
|
|
s.add(point)
|
|
|
|
for dt in {1, -1, 1j, -1j}:
|
|
np = point + dt
|
|
if not np in d: continue
|
|
if d[np] == (d[point] + 1):
|
|
q.append(np)
|
|
scores.append(len(nines))
|
|
ratings.append(nines2)
|
|
return scores if not p2 else ratings
|
|
|
|
print(sum(run()))
|
|
print(sum(run(True)))
|