day 5, part 2 in brute force

compiled with codon, runs for 7 minutes
should rewrite algorithme with range intersection
This commit is contained in:
setop 2023-12-07 02:01:47 +01:00
parent 6860e71dbd
commit baabb6cd88
1 changed files with 24 additions and 49 deletions

View File

@ -2,55 +2,30 @@ import sys
en=enumerate en=enumerate
def r2v(a,l): L:list[str] = sys.stdin.read().split('\n\n')
return (a, a+l-1) 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)
def v2r(a,b):
return (a, b-a+1)
def intersec(A:tuple, B:tuple) -> list(tuple): low = 99999999999
a0, l = A for s,r in S:
b0, m = B print(f'{s=} {r=}')
a1 = r2v(A)[1] for i in range(r):
b1 = r2v(B)[1] t = s+i
if b1 < a0 or a1 < b0: # B outside #print(f'{t=}')
return A for j,l in en(L[1:]): # for each transformation
if a0 < b0 and b1 < a1: ns = None # transformed by a range
return [v2r(a0,b0-1),B,(b1+1,a1)] #print(f'transf {j}, {l}')
if a0 == b0 and b1 < a1: for [dst, src, rg] in l:
return [B,(b1+1,a1)] #print(f'{src=} {dst=} {rg}')
if src <= t < src+rg:
if a0 < b0 and b1 < a1: ns = dst+(t-src)
return [v2r(a0,b0-1),B,(b1+1,a1)] #print(f'transformed {ns}')
if a0 < b0 and b1 < a1:
return [v2r(a0,b0-1),B,(b1+1,a1)]
if a0 < b0 and b1 < a1:
return [v2r(a0,b0-1),B,(b1+1,a1)]
if b0 <= a1 <= b1:
return (b0, a1-b0+1)
elif a1 > b1:
return B
return None
if __name__ == '__main__':
L = open(0).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: tuple(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)
for i,l in en(L[1:]): # for each transformation
print(i, l)
R = []
for s in S: # transform each seed range
ns = None # new range
for dst, src, rg in l:
if src <= s < src+rg:
ns = dst+(s-src)
break break
R.append(ns if ns else s) if ns:
S = R t = ns
print(S) low = min(low, t)
print(min(S)) print(low)