-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15_part1_row_beacon.py
94 lines (82 loc) · 2.57 KB
/
day15_part1_row_beacon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import re
import math
MIN_Y = math.inf
MAX_Y = -math.inf
MIN_X = math.inf
MAX_X = -math.inf
def get_data():
global MIN_Y
global MAX_Y
global MIN_X
global MAX_X
data = []
with open('data/day15_sensors.data') as f:
for row in f:
matches = re.findall("(?:x=|y=)(-?\d+)", row)
sensor_x, sensor_y, beacon_x, beacon_y = matches
sensor_x = int(sensor_x)
sensor_y = int(sensor_y)
beacon_x = int(beacon_x)
beacon_y = int(beacon_y)
MAX_Y = max(sensor_y, beacon_y, MAX_Y)
MIN_Y = min(sensor_y, beacon_y, MIN_Y)
MAX_X = max(sensor_x, beacon_x, MAX_X)
MIN_X = min(sensor_x, beacon_x, MIN_X)
sensor_xy = (sensor_x, sensor_y)
beacon_xy = (beacon_x, beacon_y)
data.append((sensor_xy, beacon_xy))
return data
def get_distance(a_xy, b_xy):
# manhattan distance between two coordinates
ax, ay = a_xy
bx, by = b_xy
dist_x = abs(ax - bx)
dist_y = abs(ay - by)
return dist_x + dist_y
class Sensor():
def __init__(self, sensor_xy, closest_beacon_xy) -> None:
x, y = sensor_xy
self.x = x
self.y = y
self.xy = sensor_xy
beacon_x, beacon_y = closest_beacon_xy
self.beacon_x = beacon_x
self.beacon_y = beacon_y
self.beacon_xy = closest_beacon_xy
self.beacon_distance = get_distance(self.xy, self.beacon_xy)
def main():
global MIN_X
global MAX_X
Y_TO_CHECK = 10
Y_TO_CHECK = 2000000
print('getting data')
data = get_data()
print('creating sensors')
sensors = []
for sensor_xy, beacon_xy in data:
sensor = Sensor(sensor_xy, beacon_xy)
sensors.append(sensor)
# update limits
for sensor in sensors:
MIN_X = min(MIN_X, sensor.x - sensor.beacon_distance)
MAX_X = max(MAX_X, sensor.x + sensor.beacon_distance)
print('finding impossible locations')
y = Y_TO_CHECK
impossible_locations = 0
for x in range(MIN_X, MAX_X + 1):
location_possible = True
for sensor in sensors:
if (x, y) == sensor.beacon_xy:
location_possible = True
break
if get_distance((x, y), sensor.xy) <= sensor.beacon_distance:
# print(x, y)
location_possible = False
break
if not location_possible:
impossible_locations += 1
print(f'answer: {impossible_locations}')
if __name__ == "__main__":
main()
# 4389717 too low
# 5500272 too low