-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-15.py
81 lines (63 loc) · 2.25 KB
/
day-15.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
# read input-15.txt
with open("input-15.txt") as f:
data = f.read().splitlines()
# parse lines for the like
# Sensor at x=2, y=18: closest beacon is at x=-2, y=15
line_to_check = 2000000
occupied_on_line = set()
beacons_on_line = set()
max_coord = 4000_000
min_coord = 0
occupied = dict()
for line in data:
print(line)
line = line.split(" ")
x = int(line[2].split("=")[1][:-1])
y = int(line[3].split("=")[1][:-1])
bx = int(line[8].split("=")[1][:-1])
by = int(line[9].split("=")[1])
# manhattan distance between x,y and bx,by
distance = abs(x - bx) + abs(y - by)
# find position on line y=10 which are within distance from x,y
vertical_distance = abs(y - line_to_check)
if vertical_distance <= distance:
# find x positions
for i in range(
x - (distance - vertical_distance), x + (distance - vertical_distance) + 1
):
occupied_on_line.add(i)
if by == line_to_check:
beacons_on_line.add(bx)
for ty in range(y - distance, y + distance):
_ = occupied.setdefault(ty, set())
new_coords = (x - (distance - abs(y - ty)), x + (distance - abs(y - ty)))
# check if within max min coords
if new_coords[0] <= max_coord or new_coords[1] >= min_coord:
occupied[ty].add(new_coords)
# find occupied positions
print(len(occupied_on_line.difference(beacons_on_line)))
def reduce_overlap(sorted_list, current_segment=None):
if len(sorted_list) == 0:
return current_segment
if current_segment is None:
current_segment = sorted_list[0]
return reduce_overlap(sorted_list[1:], current_segment)
line = sorted_list[0]
if line[0] <= current_segment[1] + 1:
current_segment = (current_segment[0], max(current_segment[1], line[1]))
return reduce_overlap(sorted_list[1:], current_segment)
else:
return current_segment, reduce_overlap(sorted_list[1:])
for k, v in occupied.items():
if k < min_coord or k > max_coord:
continue
ll = list(v)
ll.sort(key=lambda x: x[0])
overlap = reduce_overlap(ll)
if isinstance(overlap[0], tuple):
print(k, ll)
print(overlap)
y = k
x = overlap[0][1] + 1
break
print(x * max_coord + y)