aoc2023/d05/part2.py

32 lines
885 B
Python

import sys
en=enumerate
L:list[str] = sys.stdin.read().split('\n\n')
L = [l.split(':') for l in L]
L = [l[1].strip().split('\n') for l in L]
L = [list(map(lambda x: list(map(int,x.split())),l)) for l in L]
S = list(v for s in L[0] for v in s)
S = list(zip(S[0::2],S[1::2])) # list of range (start, length)
low = 99999999999
for s,r in S:
print(f'{s=} {r=}')
for i in range(r):
t = s+i
#print(f'{t=}')
for j,l in en(L[1:]): # for each transformation
ns = None # transformed by a range
#print(f'transf {j}, {l}')
for [dst, src, rg] in l:
#print(f'{src=} {dst=} {rg}')
if src <= t < src+rg:
ns = dst+(t-src)
#print(f'transformed {ns}')
break
if ns:
t = ns
low = min(low, t)
print(low)