diff --git a/2022/python/day15.py b/2022/python/day15.py index 59e53e0..048b63b 100644 --- a/2022/python/day15.py +++ b/2022/python/day15.py @@ -4,7 +4,10 @@ from pprint import pprint import shared from scanf import scanf from dataclasses import dataclass -from scipy.spatial.distance import cityblock + +#def fuckoff_ill_do_my_own_cityblock(y1,x1, y2,x2): +def cityblock(y1,x1, y2,x2): + return abs(y2-y1) + abs(x2-x1) @dataclass class Sensor: @@ -13,51 +16,33 @@ class Sensor: bX: int bY: int - oX: int - oY: int - @property def s(self): - return (self.oY+self.sY, self.oX+self.sX) + return (self.sY, self.sX) @property def b(self): - return (self.oY+self.bY, self.oX+self.bX) + return (self.bY, self.bX) @property def distance(self): - a,b = zip(self.s, self.b) - return cityblock(a, b) + return cityblock(self.sY,self.sX, self.bY, self.bX) - def distance_to(self, y, x): - a,b = zip(self.s, (y,x)) - return cityblock(a, b) + def distance_to(self, bY, bX): + return cityblock(self.sY,self.sX, bY, bX) def on_line(self, y): - print(self.s) midpoint = (y,self.s[1]) d = self.distance_to(*midpoint) if d > self.distance: - print() return [] - start = (y, midpoint[1] - abs(self.distance-d)) - end = (y, midpoint[1] + abs(self.distance-d)) + need = self.distance - d - assert self.distance_to(*start) == self.distance - print(self.distance_to(*start),'=',start[1],midpoint[1],end[1]) - print(self.distance_to(*end)) - print() - for x in range(start[1]+1, 9999): - d = self.distance_to(y, x) - print(x, d) - if d == self.distance: - print("X should be:", x) - break - print() + start = (y, midpoint[1] - need) + end = (y, midpoint[1] + need) - - return list(range(start[1],end[1])) + return list(range(start[1],end[1]+1)) @@ -75,56 +60,34 @@ def part1(rows): xSet.add(bx) ySet.add(y) ySet.add(by) - sensors.append(Sensor(sX=x,sY=y,bX=bx,bY=by,oY=0, oX=0)) + sensors.append(Sensor(sX=x,sY=y,bX=bx,bY=by)) minX, maxX = min(xSet),max(xSet) minY, maxY = min(ySet),max(ySet) - offsetX = 0 - offsetY = 0 - if minX < 0 or minY < 0: - o = max(abs(minX), abs(minY)) - offsetX = o - offsetY = o for sensor in sensors: - sensor.oY = offsetY - sensor.oX = offsetX beacon_points.append(sensor.b) - CHECK_ROW = 10 + offsetY - #CHECK_ROW = 2000000 + offsetY + CHECK_ROW = 10 + CHECK_ROW = 2000000 print(beacon_points) print("") ineligible = set() - for y,x in beacon_points: - if y == CHECK_ROW: - ineligible.add(x) - mx = matrix.matrix_of_size(maxX+offsetX+1, maxY+offsetY+1) for s in sensors: coll = s.on_line(CHECK_ROW) ineligible.update(coll) #print(coll) + + count_ignoring_current_beacons = 0 + for i in ineligible: + if (CHECK_ROW, i) not in beacon_points: + count_ignoring_current_beacons += 1 - for i in list(ineligible): - if i < 0: - continue - if i > maxX+offsetX: - continue - mx[CHECK_ROW][i] = "#" - for s in sensors: - mx[s.s[0]][s.s[1]] = "S" - mx[s.b[0]][s.b[1]] = "B" - #print(matrix.ppmx(mx, pad=False, space=False)) - print(" 1 1 2 2") - print(" 0 5 0 5 0 5") - matrix.view_matrix(mx, 11,0, 13, 30) - print("") - print("####B######################") - - print(ineligible) - print(len(ineligible)) + #print(ineligible) + print(len(ineligible), "without removing beacons") + print(count_ignoring_current_beacons, "with removing beacons, final answer") # mx[sensor.sY][sensor.sX] = "S"