iterate around sensor perimeters not full grid
This commit is contained in:
parent
1cdcecb7f2
commit
722c549e24
|
@ -30,13 +30,6 @@ def do_sensors(lines: list[str], row: int):
|
||||||
# do_sensors(sample_lines, 20)
|
# do_sensors(sample_lines, 20)
|
||||||
do_sensors(lines, 2_000_000)
|
do_sensors(lines, 2_000_000)
|
||||||
|
|
||||||
def in_range_of_sensors(sensors, x, y):
|
|
||||||
for (i,j,d) in sensors:
|
|
||||||
if abs(x-i) + abs(y-j) <= d:
|
|
||||||
d2 = d - abs(x-i)
|
|
||||||
y_advance = (j+d2)-y + 1
|
|
||||||
return y_advance
|
|
||||||
return False
|
|
||||||
|
|
||||||
def do_sensors_2(lines: list[str], search_space: int):
|
def do_sensors_2(lines: list[str], search_space: int):
|
||||||
sensors = []
|
sensors = []
|
||||||
|
@ -44,17 +37,27 @@ def do_sensors_2(lines: list[str], search_space: int):
|
||||||
x, y, nx, ny = line_to_numbers(line)
|
x, y, nx, ny = line_to_numbers(line)
|
||||||
distance = abs(x-nx) + abs(y-ny)
|
distance = abs(x-nx) + abs(y-ny)
|
||||||
sensors.append((x, y, distance))
|
sensors.append((x, y, distance))
|
||||||
sensors.sort(key=lambda x: x[0])
|
sensors.sort(key=lambda x: x[2]) # smallest distance first
|
||||||
|
|
||||||
|
def in_range_of_sensors(x: int, y: int) -> bool:
|
||||||
|
if not (0 <= x <= search_space and 0 <= y <= search_space):
|
||||||
|
return True
|
||||||
|
for (i,j,d) in sensors:
|
||||||
|
if abs(x-i) + abs(y-j) <= d:
|
||||||
|
return True
|
||||||
|
print(f'Part 2: Seen at ({x},{y}) = submit {x*4_000_000 + y}')
|
||||||
|
return False
|
||||||
|
|
||||||
# print(sensors)
|
# print(sensors)
|
||||||
for x in range(0, search_space+1):
|
for x0, y0, d in sensors: # Iterate around perimeters
|
||||||
y = 0
|
# print(x0, y0, d)
|
||||||
while y <= search_space:
|
y_up = y0 + d + 1
|
||||||
y_skip = in_range_of_sensors(sensors, x, y)
|
y_down = y0 - d - 1
|
||||||
if y_skip is False:
|
for i in range(d+1):
|
||||||
print(f'Part 2: Seen at ({x},{y}) = submit {x*4000000 + y}')
|
for x in (x0+i, x0-i):
|
||||||
|
for y in (y_down+i, y_up-i):
|
||||||
|
if not in_range_of_sensors(x, y):
|
||||||
return
|
return
|
||||||
y += y_skip
|
|
||||||
|
|
||||||
# do_sensors_2(sample_lines, 20)
|
# do_sensors_2(sample_lines, 20)
|
||||||
do_sensors_2(lines, 4_000_000)
|
do_sensors_2(lines, 4_000_000)
|
||||||
|
|
Loading…
Reference in New Issue