day 15, almost there but wrong answer. big up for Codon

This commit is contained in:
setop 2022-12-15 15:42:32 +01:00
parent efc1f11758
commit e03e062899
5 changed files with 229 additions and 0 deletions

40
d15/draw.py Normal file
View File

@ -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('}')

52
d15/part1.py Normal file
View File

@ -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)

53
d15/part2a.py Normal file
View File

@ -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<W and ny>=0 and ny<H:
M[ny][nx] = 0
for y,r in enumerate(M):
for x,c in enumerate(r):
if c == 0:
if len(P)>20:
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

37
d15/part2b.py Normal file
View File

@ -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

47
d15/part2c.py Normal file
View File

@ -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