day 5, part2 not achieved

This commit is contained in:
setop 2023-12-06 09:57:38 +01:00
parent c78f8ebf61
commit 2ae942085d
2 changed files with 80 additions and 0 deletions

24
d05/part1.py Normal file
View File

@ -0,0 +1,24 @@
import sys
en=enumerate
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)
for i,l in en(L[1:]):
print(i, l)
# map all seads
R = []
for s in S:
ns = None
for dst, src, rg in l:
if src <= s < src+rg:
ns = dst+(s-src)
break
R.append(ns if ns else s)
S = R
print(S)
print(min(S))

56
d05/part2.py Normal file
View File

@ -0,0 +1,56 @@
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 = [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
R.append(ns if ns else s)
S = R
print(S)
print(min(S))