Add solution for day 15

This commit is contained in:
Maya Herrscher committed 2022-12-16 01:45:21 +01:00
1 parent 7104e413c0
commit 63e49648c0
4 files changed
+116

No files matched your search

View File
Whitespace-only changes.
+71
View File
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
import sys, re
from collections import defaultdict
MAP = defaultdict(lambda: defaultdict(lambda: '.'))
SENSORS = {}
MINX = 0
MAXX = 0
def add_sensor(line):
groups = re.compile(r"Sensor at x=(?P<sx>-?\d+), y=(?P<sy>-?\d+): closest beacon is at x=(?P<bx>-?\d+), y=(?P<by>-?\d+)").search(line)
sx = int(groups.group("sx"))
sy = int(groups.group("sy"))
bx = int(groups.group("bx"))
by = int(groups.group("by"))
SENSORS[(sx,sy)] = (bx,by)
MAP[sy][sx] = 'S'
MAP[by][bx] = 'B'
def cross_out(y):
for sx,sy in SENSORS.keys():
bx, by = SENSORS[(sx,sy)]
global MINX, MAXX
d = abs(sx - bx) + abs(sy - by)
if bx > MAXX: MAXX = bx
if sx + d > MAXX: MAXX = sx + d
if bx < MINX: MINX = bx
if sx - d < MINX: MINX = sx - d
if y in range(sy-d,sy+d+1):
dif = d - abs(sy-y)
for x in range(sx-dif,sx+dif):
MAP[y][x] = '#'
# for the chsllenge, needs to be called with './code.py input 2000000'
if __name__ == '__main__':
inp = [add_sensor(line.strip('\n')) for line in open(sys.argv[1])]
y = int(sys.argv[2])
print(SENSORS)
# challenge 1
cross_out(y)
no_beacon = 0
for x in range(MINX,MAXX+1):
if MAP[y][x] == '#': no_beacon += 1
res1 = str(no_beacon)
print("challenge 1:" + "\n" + res1 + "\n")
# challenge 2
tf = 0
for j in range(0,2*y+1):
i = 0
while i < 2*y+1:
b = ''
for sx,sy in SENSORS.keys():
bx, by = SENSORS[(sx,sy)]
d = abs(sx - bx) + abs(sy - by)
dy = max(0,d - abs(sx-i))
dif = max(0,d - abs(sy-j))
if j in range(sy-dy,sy+dy+1) and i in range(sx-dif,sx+dif+1):
b = 'y'
i = sx + dif + 1
if b == 'y': continue
tf = i*4000000 + j
print(i,j,tf)
break
res2 = str(tf)
print("challenge 2:" + "\n" + res2 + "\n")
+14
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
+31
View File
@@ -0,0 +1,31 @@
Sensor at x=3923513, y=2770279: closest beacon is at x=3866712, y=2438950
Sensor at x=675683, y=3223762: closest beacon is at x=-224297, y=2997209
Sensor at x=129453, y=2652332: closest beacon is at x=92656, y=2629486
Sensor at x=3906125, y=2154618: closest beacon is at x=3866712, y=2438950
Sensor at x=65723, y=902062: closest beacon is at x=92656, y=2629486
Sensor at x=3137156, y=2876347: closest beacon is at x=2907507, y=3100765
Sensor at x=32848, y=2676435: closest beacon is at x=92656, y=2629486
Sensor at x=3272472, y=3445147: closest beacon is at x=2907507, y=3100765
Sensor at x=2926008, y=128948: closest beacon is at x=3089364, y=-501737
Sensor at x=2975, y=2769838: closest beacon is at x=92656, y=2629486
Sensor at x=3540455, y=2469135: closest beacon is at x=3866712, y=2438950
Sensor at x=3674809, y=2062166: closest beacon is at x=3719980, y=2000000
Sensor at x=3693706, y=2027384: closest beacon is at x=3719980, y=2000000
Sensor at x=3869683, y=2291983: closest beacon is at x=3866712, y=2438950
Sensor at x=2666499, y=2796436: closest beacon is at x=2650643, y=2489479
Sensor at x=492, y=2601991: closest beacon is at x=92656, y=2629486
Sensor at x=2710282, y=3892347: closest beacon is at x=2907507, y=3100765
Sensor at x=28974, y=3971342: closest beacon is at x=-224297, y=2997209
Sensor at x=3990214, y=2399722: closest beacon is at x=3866712, y=2438950
Sensor at x=3853352, y=1009020: closest beacon is at x=3719980, y=2000000
Sensor at x=1231833, y=3999338: closest beacon is at x=1313797, y=4674300
Sensor at x=2083669, y=875035: closest beacon is at x=1369276, y=-160751
Sensor at x=1317274, y=2146819: closest beacon is at x=2650643, y=2489479
Sensor at x=3712875, y=2018770: closest beacon is at x=3719980, y=2000000
Sensor at x=963055, y=23644: closest beacon is at x=1369276, y=-160751
Sensor at x=3671967, y=64054: closest beacon is at x=3089364, y=-501737
Sensor at x=3109065, y=2222392: closest beacon is at x=2650643, y=2489479
Sensor at x=3218890, y=1517419: closest beacon is at x=3719980, y=2000000
Sensor at x=3856777, y=3987650: closest beacon is at x=4166706, y=3171774
Sensor at x=1912696, y=3392788: closest beacon is at x=2907507, y=3100765
Sensor at x=3597620, y=3100104: closest beacon is at x=4166706, y=3171774