From 2ae942085db32047c4cc4d4050977b77c0372ac9 Mon Sep 17 00:00:00 2001 From: setop Date: Wed, 6 Dec 2023 09:57:38 +0100 Subject: [PATCH] day 5, part2 not achieved --- d05/part1.py | 24 ++++++++++++++++++++++ d05/part2.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 d05/part1.py create mode 100644 d05/part2.py diff --git a/d05/part1.py b/d05/part1.py new file mode 100644 index 0000000..e020a77 --- /dev/null +++ b/d05/part1.py @@ -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)) diff --git a/d05/part2.py b/d05/part2.py new file mode 100644 index 0000000..2b9e040 --- /dev/null +++ b/d05/part2.py @@ -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))