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
def r2v(a,l):
return (a, a+l-1)
def v2r(a,b):
return (a, b-a+1)
def intersec(A:tuple, B:tuple) -> list(tuple):
a0, l = A
b0, m = B
a1 = r2v(A)[1]
b1 = r2v(B)[1]
if b1 < a0 or a1 < b0: # B outside
return A
if a0 < b0 and b1 < a1:
return [v2r(a0,b0-1),B,(b1+1,a1)]
if a0 == b0 and b1 < a1:
return [B,(b1+1,a1)]
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 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: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: tuple(map(int,x.split())),l)) 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)
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)
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
R.append(ns if ns else s)
S = R
print(S)
print(min(S))
if ns:
t = ns
low = min(low, t)
print(low)