Add solution for day 15
This commit is contained in:
1 parent
7104e413c0
commit
63e49648c0
4 files changed
+116
No files matched your search
Executable
+71
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user