aoc2023/d11/run.py

28 lines
782 B
Python
Raw Permalink Normal View History

2023-12-11 11:36:22 +00:00
import sys
from itertools import combinations as Comb
I = sys.stdin.read().splitlines() # for codon type inference
L = [list(l) for l in I]
H = len(L)
W = len(L[0])
RD = [y for y in range(W) if all(L[y][x] == '.' for x in range(H))]
CD = [x for x in range(H) if all(L[y][x] == '.' for y in range(W))]
G = [(x,y) for y in range(W) for x in range(H) if L[y][x] == '#']
# expantion factor, 2 for part 1, 1E6 for part 2
X = int(float(sys.argv[1])) # float to parse scientific form, int for codon
def exp(a:int, b:int, S:list[int]) -> int:
if a > b:
b, a = a, b
return sum(1 for s in S if a<s<b)
S = 0
for (x1,y1),(x2,y2) in Comb(G,2):
ex = exp(x1, x2, CD)
ey = exp(y1, y2, RD)
d = abs(x2-x1) + abs(y2-y1) + (X-1)*(ex+ey)
S += d
print(int(S))