Day15 part 1 done

This commit is contained in:
Tyrel Souza 2022-12-15 11:32:45 -05:00
parent 3e0ae4efee
commit 00acd198d1

View File

@ -4,7 +4,10 @@ from pprint import pprint
import shared import shared
from scanf import scanf from scanf import scanf
from dataclasses import dataclass 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 @dataclass
class Sensor: class Sensor:
@ -13,51 +16,33 @@ class Sensor:
bX: int bX: int
bY: int bY: int
oX: int
oY: int
@property @property
def s(self): def s(self):
return (self.oY+self.sY, self.oX+self.sX) return (self.sY, self.sX)
@property @property
def b(self): def b(self):
return (self.oY+self.bY, self.oX+self.bX) return (self.bY, self.bX)
@property @property
def distance(self): def distance(self):
a,b = zip(self.s, self.b) return cityblock(self.sY,self.sX, self.bY, self.bX)
return cityblock(a, b)
def distance_to(self, y, x): def distance_to(self, bY, bX):
a,b = zip(self.s, (y,x)) return cityblock(self.sY,self.sX, bY, bX)
return cityblock(a, b)
def on_line(self, y): def on_line(self, y):
print(self.s)
midpoint = (y,self.s[1]) midpoint = (y,self.s[1])
d = self.distance_to(*midpoint) d = self.distance_to(*midpoint)
if d > self.distance: if d > self.distance:
print()
return [] return []
start = (y, midpoint[1] - abs(self.distance-d)) need = self.distance - d
end = (y, midpoint[1] + abs(self.distance-d))
assert self.distance_to(*start) == self.distance start = (y, midpoint[1] - need)
print(self.distance_to(*start),'=',start[1],midpoint[1],end[1]) end = (y, midpoint[1] + need)
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()
return list(range(start[1],end[1]+1))
return list(range(start[1],end[1]))
@ -75,56 +60,34 @@ def part1(rows):
xSet.add(bx) xSet.add(bx)
ySet.add(y) ySet.add(y)
ySet.add(by) 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) minX, maxX = min(xSet),max(xSet)
minY, maxY = min(ySet),max(ySet) 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: for sensor in sensors:
sensor.oY = offsetY
sensor.oX = offsetX
beacon_points.append(sensor.b) beacon_points.append(sensor.b)
CHECK_ROW = 10 + offsetY CHECK_ROW = 10
#CHECK_ROW = 2000000 + offsetY CHECK_ROW = 2000000
print(beacon_points) print(beacon_points)
print("") print("")
ineligible = set() 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: for s in sensors:
coll = s.on_line(CHECK_ROW) coll = s.on_line(CHECK_ROW)
ineligible.update(coll) ineligible.update(coll)
#print(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(ineligible)
print(" 1 1 2 2") print(len(ineligible), "without removing beacons")
print(" 0 5 0 5 0 5") print(count_ignoring_current_beacons, "with removing beacons, final answer")
matrix.view_matrix(mx, 11,0, 13, 30)
print("")
print("####B######################")
print(ineligible)
print(len(ineligible))
# mx[sensor.sY][sensor.sX] = "S" # mx[sensor.sY][sensor.sX] = "S"