day 15 part 1 - ATTEMPT
This commit is contained in:
parent
17652da8ee
commit
1d208febef
@ -1,15 +1,26 @@
|
|||||||
|
import matrix
|
||||||
import shared
|
import shared
|
||||||
from pprint import pprint as pp
|
import itertools
|
||||||
|
import functools
|
||||||
|
|
||||||
|
#@shared.profile
|
||||||
|
def part1(rows):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def run(SOMETHING):
|
|
||||||
pp(SOMETHING)
|
|
||||||
|
|
||||||
|
#@shared.profile
|
||||||
|
def part2(rows):
|
||||||
|
pass
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
spl = lambda y: [int(w) for w in y]
|
rows = [row for row in shared.load_rows(15)]
|
||||||
rows = [row.rstrip().split() for row in shared.load(3)]
|
with shared.elapsed_timer() as elapsed:
|
||||||
print(run(rows))
|
part1(rows)
|
||||||
|
print("🕒", elapsed())
|
||||||
|
with shared.elapsed_timer() as elapsed:
|
||||||
|
part2(rows)
|
||||||
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
100
2022/python/day15.py
Normal file
100
2022/python/day15.py
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import matrix
|
||||||
|
import math
|
||||||
|
from pprint import pprint
|
||||||
|
import shared
|
||||||
|
from scanf import scanf
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from scipy.spatial.distance import cityblock
|
||||||
|
|
||||||
|
def manhattan(a, b):
|
||||||
|
return abs(b[0] - a[0]) + abs(b[1] - a[1])
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Sensor:
|
||||||
|
sX: int
|
||||||
|
sY: int
|
||||||
|
bX: int
|
||||||
|
bY: int
|
||||||
|
|
||||||
|
@property
|
||||||
|
def s(self):
|
||||||
|
return (self.sY, self.sX)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def b(self):
|
||||||
|
return (self.bY, self.bX)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def distance(self):
|
||||||
|
a,b = zip(self.s, self.b)
|
||||||
|
return cityblock(a, b)
|
||||||
|
|
||||||
|
def distance_to(self, y,x):
|
||||||
|
a,b = zip(self.s, (y,x))
|
||||||
|
return cityblock(a, b)
|
||||||
|
|
||||||
|
|
||||||
|
#@shared.profile
|
||||||
|
def part1(rows):
|
||||||
|
CHECK_ROW = 10
|
||||||
|
CHECK_ROW = 2000000
|
||||||
|
sensors = []
|
||||||
|
sensor_points = []
|
||||||
|
beacon_points = []
|
||||||
|
xSet = set()
|
||||||
|
ySet = set()
|
||||||
|
for row in rows:
|
||||||
|
x,y,bx,by = scanf("Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", row)
|
||||||
|
xSet.add(x)
|
||||||
|
xSet.add(bx)
|
||||||
|
ySet.add(y)
|
||||||
|
ySet.add(by)
|
||||||
|
sensors.append(Sensor(sX=x,sY=y,bX=bx,bY=by))
|
||||||
|
minX, maxX = min(xSet),max(xSet)
|
||||||
|
minY, maxY = min(ySet),max(ySet)
|
||||||
|
for sensor in sensors:
|
||||||
|
sensor_points.append(sensor.s)
|
||||||
|
beacon_points.append(sensor.b)
|
||||||
|
|
||||||
|
ineligible = set()
|
||||||
|
for x in range(minX,maxX):
|
||||||
|
point = (CHECK_ROW, x)
|
||||||
|
if x % 10000 == 0:
|
||||||
|
print(point)
|
||||||
|
if point in beacon_points:
|
||||||
|
continue
|
||||||
|
if point in sensor_points:
|
||||||
|
continue
|
||||||
|
for s in sensors:
|
||||||
|
d = s.distance_to(*point)
|
||||||
|
if d <= s.distance:
|
||||||
|
ineligible.add(point)
|
||||||
|
pprint(ineligible)
|
||||||
|
print(len(ineligible))
|
||||||
|
|
||||||
|
# mx[sensor.sY][sensor.sX] = "S"
|
||||||
|
# mx[sensor.bY][sensor.bX] = "B"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#@shared.profile
|
||||||
|
def part2(rows):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def main():
|
||||||
|
rows = [row for row in shared.load_rows(15)]
|
||||||
|
|
||||||
|
with shared.elapsed_timer() as elapsed:
|
||||||
|
part1(rows)
|
||||||
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
with shared.elapsed_timer() as elapsed:
|
||||||
|
part2(rows)
|
||||||
|
print("🕒", elapsed())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -154,7 +154,7 @@ def get_neighbor_coords(matrix, c, r, diagonals=True):
|
|||||||
return neighbors
|
return neighbors
|
||||||
|
|
||||||
|
|
||||||
def line_of_sight_coords(matrix, row, col) -> Dict[str, List[Tuple[int, int]]]:
|
def line_of_sight_coords(matrix, row, col, distance=None) -> Dict[str, List[Tuple[int, int]]]:
|
||||||
"""
|
"""
|
||||||
Takes a matrix, a row, and a column
|
Takes a matrix, a row, and a column
|
||||||
calculates the coordinates to the edge for all four cardinal directions
|
calculates the coordinates to the edge for all four cardinal directions
|
||||||
@ -167,6 +167,10 @@ def line_of_sight_coords(matrix, row, col) -> Dict[str, List[Tuple[int, int]]]:
|
|||||||
col_ids = list(range(0, height))
|
col_ids = list(range(0, height))
|
||||||
row_ids = list(range(0, width))
|
row_ids = list(range(0, width))
|
||||||
|
|
||||||
|
if distance:
|
||||||
|
up_ids, down_ids = list(reversed(col_ids[:col])), col_ids[col + 1 :col+distance+1]
|
||||||
|
left_ids, right_ids = list(reversed(row_ids[:row])), row_ids[row + 1 :row+distance+1]
|
||||||
|
else:
|
||||||
up_ids, down_ids = list(reversed(col_ids[:col])), col_ids[col + 1 :]
|
up_ids, down_ids = list(reversed(col_ids[:col])), col_ids[col + 1 :]
|
||||||
left_ids, right_ids = list(reversed(row_ids[:row])), row_ids[row + 1 :]
|
left_ids, right_ids = list(reversed(row_ids[:row])), row_ids[row + 1 :]
|
||||||
|
|
||||||
@ -183,11 +187,11 @@ def line_of_sight_coords(matrix, row, col) -> Dict[str, List[Tuple[int, int]]]:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def line_of_sight(mx, row, col):
|
def line_of_sight(mx, row, col, distance=None):
|
||||||
"""
|
"""
|
||||||
renders a line of sight coord calculation, into the values
|
renders a line of sight coord calculation, into the values
|
||||||
"""
|
"""
|
||||||
coords = line_of_sight_coords(mx, row, col)
|
coords = line_of_sight_coords(mx, row, col, distance)
|
||||||
los = defaultdict(list)
|
los = defaultdict(list)
|
||||||
for k, ids in coords.items():
|
for k, ids in coords.items():
|
||||||
for _row, _col in ids:
|
for _row, _col in ids:
|
||||||
@ -195,7 +199,6 @@ def line_of_sight(mx, row, col):
|
|||||||
return los
|
return los
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def coords_between_points(point1, point2):
|
def coords_between_points(point1, point2):
|
||||||
y1,x1 = point1
|
y1,x1 = point1
|
||||||
y2,x2 = point2
|
y2,x2 = point2
|
||||||
@ -225,9 +228,6 @@ def coords_between_points(point1, point2):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_size(matrix):
|
def get_size(matrix):
|
||||||
height = len(matrix)
|
height = len(matrix)
|
||||||
width = len(matrix[0])
|
width = len(matrix[0])
|
||||||
@ -337,3 +337,5 @@ def highlight(matrix, red=[], green=[], blue=[], blink_green=[]):
|
|||||||
new = f"{colors.BLINK}{colors.GREEN}{mx[y][x]}{colors.ENDC}"
|
new = f"{colors.BLINK}{colors.GREEN}{mx[y][x]}{colors.ENDC}"
|
||||||
mx[y][x] = new
|
mx[y][x] = new
|
||||||
ppmx(mx, pad=False, space=True, zero="0")
|
ppmx(mx, pad=False, space=True, zero="0")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user