Compare commits

...

4 Commits

Author SHA1 Message Date
magical 7b40516b66 day 15 part 2 alternate solution 2022-12-15 12:55:42 -08:00
magical 2b0ef33a08 day 15 python wip optimizations 2022-12-14 22:50:07 -08:00
magical 5f76b4e69d day 15 python part 2 2022-12-14 22:47:37 -08:00
magical af21ad12cc day 15 python part 1 2022-12-14 21:26:18 -08:00
4 changed files with 142 additions and 0 deletions

35
day15/clip.py 100644
View File

@ -0,0 +1,35 @@
import pyclipper # https://pypi.org/project/pyclipper/
data = []
for line in open("input"):
words = line.replace(":", "").replace(",","").split()
coords = [int(w[w.index('=')+1:]) for w in words if '=' in w]
data.append(coords)
pc = pyclipper.Pyclipper()
for sx,sy,bx,by in data:
dx = abs(sx-bx)
dy = abs(sy-by)
d = dx+dy
pc.AddPath([(sx+d,sy),(sx,sy+d),(sx-d,sy),(sx,sy-d)], pyclipper.PT_CLIP, closed=True)
M = 4000000
pc.AddPath([(0,0),(M,0),(M,M),(0,M)], pyclipper.PT_SUBJECT, closed=True)
solution = pc.Execute(pyclipper.CT_DIFFERENCE, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)
# should return a single small polygon shaped like a diamond
# the answer we want is the point at the center of the diamond
assert len(solution) == 1
assert pyclipper.Area(solution[0]) < 4
def avg(ns):
return sum(ns)/len(ns)
for p in solution:
x = avg([x for x,y in p])
y = avg([y for x,y in p])
print(x,y)
print("frequency =",M*x + y)

23
day15/input 100644
View File

@ -0,0 +1,23 @@
Sensor at x=325337, y=2568863: closest beacon is at x=-518661, y=2000000
Sensor at x=3988825, y=837820: closest beacon is at x=4305648, y=2127118
Sensor at x=1611311, y=2053174: closest beacon is at x=2827226, y=1579510
Sensor at x=101890, y=3940049: closest beacon is at x=955472, y=3457514
Sensor at x=3962702, y=2558425: closest beacon is at x=4226981, y=2604726
Sensor at x=2957890, y=2160813: closest beacon is at x=2827226, y=1579510
Sensor at x=3907456, y=3325610: closest beacon is at x=3696221, y=3226373
Sensor at x=3354177, y=3435919: closest beacon is at x=3696221, y=3226373
Sensor at x=3997379, y=3071868: closest beacon is at x=3696221, y=3226373
Sensor at x=145143, y=1714962: closest beacon is at x=-518661, y=2000000
Sensor at x=611563, y=3148864: closest beacon is at x=955472, y=3457514
Sensor at x=3080405, y=3904777: closest beacon is at x=3696221, y=3226373
Sensor at x=644383, y=10732: closest beacon is at x=364635, y=-294577
Sensor at x=3229566, y=1694167: closest beacon is at x=2827226, y=1579510
Sensor at x=1600637, y=3984884: closest beacon is at x=955472, y=3457514
Sensor at x=2959765, y=2820860: closest beacon is at x=2491502, y=2897876
Sensor at x=2235330, y=3427797: closest beacon is at x=2491502, y=2897876
Sensor at x=2428996, y=210881: closest beacon is at x=2827226, y=1579510
Sensor at x=369661, y=687805: closest beacon is at x=364635, y=-294577
Sensor at x=3558476, y=2123614: closest beacon is at x=4305648, y=2127118
Sensor at x=3551529, y=2825104: closest beacon is at x=3696221, y=3226373
Sensor at x=64895, y=3577: closest beacon is at x=364635, y=-294577
Sensor at x=3079531, y=1538659: closest beacon is at x=2827226, y=1579510

14
day15/sample 100644
View File

@ -0,0 +1,14 @@
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3

70
day15/sol.py 100644
View File

@ -0,0 +1,70 @@
data = []
for line in open("input"):
words = line.replace(":", "").replace(",","").split()
coords = [int(w[w.index('=')+1:]) for w in words if '=' in w]
data.append(coords)
#print(data)
none = set()
Y = 10
Y = 2000000
for sx,sy,bx,by in data:
dx = abs(sx-bx)
dy = abs(sy-by)
dist = dx+dy
y = Y-sy
#for x in range(-dist,dist+1):
# if abs(x)+abs(y) <= dist:
# p = (sx+x, sy+y)
# if p != (bx,by):
# none.add(sx+x)
print(len(none))
objs = []
for sx,sy,bx,by in data:
dist = abs(sx-bx) + abs(sy-by)
objs.append((sx,sy,dist))
# find the closest sensor to x,y within dist
def mindist(x,y, objs=objs):
for sx,sy,dist in yobjs:
d = abs(x-sx) + abs(y-sy)
if d <= dist:
yield dist-d
# y=2573243
Y = 2500000
Y = 0
objs.sort()
for y in range(Y, 4000000+1):
ranges = []
for sx,sy,dist in objs:
dy = abs(y-sy)
if dy <= dist:
dx = dist - dy
ranges.append((sx-dx,sx+dx))
ranges.sort()
x = 0
for a,b in ranges:
if b < x:
continue
if x < a:
print(x,y,"=",x*4000000+y)
break
x = b+1
if x > 4000000:
break
#try:
# d = max(mindist(x,y,objs=yobjs))
# x += d+1
#except ValueError:
# print(x,y)
# break
if y % 100000 == 0:
print(y)