-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
54 lines (43 loc) · 1.17 KB
/
day15.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
import re
DATA = open('day15.txt').read()
sensors = []
for line in DATA.splitlines():
m = re.match(r'Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)', line)
sensors.append(tuple(map(int, m.groups())))
def get_intervals(y):
intervals = []
for sx, sy, bx, by in sensors:
r = abs(sx - bx) + abs(sy - by)
l = r - abs(sy - y)
if l < 0:
continue
intervals.append((sx - l, sx + l))
intervals.sort()
return intervals
def part1(y):
result = 0
intervals = get_intervals(y)
ca, cb = intervals.pop(0)
for a, b in intervals:
if a > cb:
result += cb - ca
ca, cb = a, b
else:
cb = max(cb, b)
result += cb - ca
return result
def part2(area):
for y in range(area + 1):
intervals = get_intervals(y) + [(area, float('+inf'))]
cb = 0
for a, b in intervals:
if a > cb:
return (cb + 1) * 4000000 + y
else:
cb = max(cb, b)
result = part1(2000000)
print(result)
assert result == 4424278
result = part2(4000000)
print(result)
assert result == 10382630753392