advent2023/eleven.py

31 lines
726 B
Python
Executable File

#!/usr/bin/env python3
from useful import *
m = np.array([[c == '#' for c in l] for l in lines(open(0))]).astype('int')
for row in range(m.shape[0]-1, -1, -1):
if not m[row].any():
m[row] = 2
for col in range(m.shape[1]-1, -1, -1):
if not (m[:,col] == 1).any():
m[:,col] = 2
stars = zip(*np.nonzero(m == 1))
pairs = list(combinations(stars, 2))
def dist(a, b, mul):
acc = 0
y, x = min(a[0], b[0]), min(a[1], b[1])
while y < max(a[0], b[0]):
y += 1
acc += mul if m[y,x] == 2 else 1
while x < max(a[1], b[1]):
x += 1
acc += mul if m[y,x] == 2 else 1
return acc
print(sum(dist(*p, 2) for p in pairs))
print(sum(dist(*p, 1000000) for p in pairs))