From e03e062899bf626c1739d112f62a4e7ad1464004 Mon Sep 17 00:00:00 2001 From: setop Date: Thu, 15 Dec 2022 15:42:32 +0100 Subject: [PATCH] day 15, almost there but wrong answer. big up for Codon --- d15/draw.py | 40 ++++++++++++++++++++++++++++++++++++++ d15/part1.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ d15/part2a.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ d15/part2b.py | 37 +++++++++++++++++++++++++++++++++++ d15/part2c.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 229 insertions(+) create mode 100644 d15/draw.py create mode 100644 d15/part1.py create mode 100644 d15/part2a.py create mode 100644 d15/part2b.py create mode 100644 d15/part2c.py diff --git a/d15/draw.py b/d15/draw.py new file mode 100644 index 0000000..9b8eaf5 --- /dev/null +++ b/d15/draw.py @@ -0,0 +1,40 @@ +import sys +from collections import defaultdict + + +S = set() +B = set() +P = dict() +Q = defaultdict(list) + +for l in sys.stdin.read().splitlines(): + (_,_,X,Y,_,_,_,_,A,J) = l.split(" ") + x = int(X[2:-1]) + y = int(Y[2:-1]) + a = int(A[2:-1]) + b = int(J[2:]) + S.add((x,y)) + B.add((a,b)) + P[(x,y)] = (a,b) + Q[(a,b)].append((x,y)) + +if False: + print("digraph G {") + print("overlap = false;") + def toid(x): + (x,y) = x + return str(x).replace("-","m")+"_"+str(y).replace("-","m") + for (k,v) in P.items(): + print("S_"+toid(k), "->", "B_"+toid(v),";") + print('}') + +if True: + print("digraph G {") + print("overlap = false;") + def toid(x): + (x,y) = x + return str(x).replace("-","m")+"_"+str(y).replace("-","m") + for (k,V) in Q.items(): + for v in V: + print("B_"+toid(k), "->", "C_"+toid(v),";") + print('}') diff --git a/d15/part1.py b/d15/part1.py new file mode 100644 index 0000000..90fb93e --- /dev/null +++ b/d15/part1.py @@ -0,0 +1,52 @@ +import sys +from collections import defaultdict + +BX = 1765036 +BY = 2000000 + +def mtn(P,Q): + (x,y) = P + (u,v) = Q + return abs(u-x)+abs(v-y) + +S = set() +B = set() +P = dict() +Q = defaultdict(list) + +for l in sys.stdin.read().splitlines(): + (_,_,X,Y,_,_,_,_,A,J) = l.split(" ") + x = int(X[2:-1]) + y = int(Y[2:-1]) + a = int(A[2:-1]) + b = int(J[2:]) + S.add((x,y)) + B.add((a,b)) + P[(x,y)] = (a,b) + Q[(a,b)].append((x,y)) + +if len(S) < 20: + BX = 2 + BY = 10 + + +Xs = set() +for s in S: + (x,y) = s + sB = P[s] # nearest bc + dB = mtn(s,sB) # max range before first bc + dy = abs(y-BY) # to reach THE bc + r = dB - dy + if len(S) < 20: + print(s, sB, (BX,BY), dB, dy, r) + if r > 0: + for d in range(-r,r+1): + nx = x+d + if abs(nx-BX)>=0: + Xs.add(nx) + +print(len(Xs)-1) + +if len(S) < 20: + print(Xs) + diff --git a/d15/part2a.py b/d15/part2a.py new file mode 100644 index 0000000..1e8d1a6 --- /dev/null +++ b/d15/part2a.py @@ -0,0 +1,53 @@ +import sys +from collections import defaultdict + + +W = H = 4*1000*1000+1 + +def mtn(P,Q): + (x,y) = P + (u,v) = Q + return abs(u-x)+abs(v-y) + +P = dict() + +for l in sys.stdin.read().splitlines(): + (_,_,X,Y,_,_,_,_,A,J) = l.split(" ") + x = int(X[2:-1]) #+ 2 + y = int(Y[2:-1]) + a = int(A[2:-1]) #+ 2 + b = int(J[2:]) + P[(x,y)] = (a,b) + +if len(P) < 20: + print("S -> B", P) + + +if len(P) < 20: + W = H = 30 + +M = [ [1 for _ in range(W) ] for _ in range(H) ] + + +for s,sB in P.items(): + (x,y) = s + dB = mtn(s,sB) # max range before first bc + if len(P) < 20: + print(s, sB, dB) + for rx in range(dB+1): + for ry in range(dB-rx+1): + for (nx,ny) in [(x-rx,y-ry),(x-rx,y+ry),(x+rx,y-ry),(x+rx,y+ry)]: + if nx>=0 and nx=0 and ny20: + print(x,y) + pass + +if len(P) < 20: + print("\n".join(" ".join(str(i) for i in l) for l in M)) + +# take too much memory diff --git a/d15/part2b.py b/d15/part2b.py new file mode 100644 index 0000000..df75a0f --- /dev/null +++ b/d15/part2b.py @@ -0,0 +1,37 @@ +import sys +from collections import defaultdict + +def mtn(P,Q): + (x,y) = P + (u,v) = Q + return abs(u-x)+abs(v-y) + +S = list() +for l in sys.stdin.read().splitlines(): + (_,_,X,Y,_,_,_,_,A,J) = l.split(" ") + x = int(X[2:-1]) + y = int(Y[2:-1]) + a = int(A[2:-1]) + b = int(J[2:]) + S.append((x,y,abs(x-a)+abs(y-b))) + +print(S) + +W = H = 4*1000*1000 #+1 +if len(sys.argv)>1: + W = H = 20+1 + +for i in range(W): + print(".", end ='') + for j in range(H): + # is this point reachable by any sensor + r = False + for (x,y,db) in S: + dp = abs(x-i)+abs(y-j) + if dp <= db: + r = True + break + if not r: + print(i,j) + +# taking too long diff --git a/d15/part2c.py b/d15/part2c.py new file mode 100644 index 0000000..9f252f5 --- /dev/null +++ b/d15/part2c.py @@ -0,0 +1,47 @@ +import sys + +W = H = 4_000_000 +if len(sys.argv)>1: # sample + W = H = 20 + +# store position that are not reachable, row by row +L = [ [(0,0) for _ in range(32)] for _ in range(H+1) ] + + +for i,l in enumerate(sys.stdin.read().splitlines()): + (_,_,X,Y,_,_,_,_,A,J) = l.split(" ") + x = int(X[2:-1]) + y = int(Y[2:-1]) + a = int(A[2:-1]) + b = int(J[2:]) + d = abs(x-a)+abs(y-b) + for ny in range(y-d, y+d+1): + if ny>=0 and ny<=H: + dy = abs(ny-y) + dx = d-dy + L[ny][i] = (x-dx,x+dx) + #L[ny] = intesec(L[ny], (x-dy,x+dy)) + + +if len(sys.argv)>1: # sample + for l in L: + print(l) + +d=0 +for (y,I) in enumerate(L): + SI = sorted(I) + maxx = 0 + for (a,b) in SI: + if a>maxx+1: + d+=1 + print(y, maxx+1, (maxx+1)*4_000_000+y) + maxx = max(maxx,b) +print(d) # should be 1 + +#< 2_855_041 2_911_365 11645462855041 +#< 11645462855040 +#< 2_855_041 2_911_363 11645454855041 +#X 2_855_040 2_911_363 11645454855040 +#> 2_855_041 2_911_362 11645450855041 + +# terminate well but wrong answer