-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
143 lines (117 loc) · 4.39 KB
/
__init__.py
File metadata and controls
143 lines (117 loc) · 4.39 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from __future__ import annotations
import sys
from typing import *
from aocpy import BaseChallenge, Vector, foldl
import re
from dataclasses import dataclass
import portion as P
@dataclass
class Line:
m: int
c: int
@staticmethod
def from_points(a: Vector, b: Vector) -> Line:
calc_m = (b.y - a.y) / (b.x - a.x)
calc_c = a.y - (calc_m * a.x)
return Line(int(calc_m), int(calc_c))
parse_re = re.compile(
r"Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)"
)
def parse(instr: str) -> List[Tuple[Vector, Vector]]:
res = []
for line in instr.strip().splitlines():
sensor_x, sensor_y, beacon_x, beacon_y = map(int, parse_re.match(line).groups())
res.append(
(
Vector(sensor_x, sensor_y),
Vector(beacon_x, beacon_y),
)
)
return res
def postprocess(
inp: List[Tuple[Vector, Vector]]
) -> Tuple[
Dict[Vector, Tuple[Vector, Vector, Vector, Vector]],
Dict[Vector, Tuple[Line, Line, Line, Line]],
]:
# 0 top, 1 left, 2 right, 3 bottom
sensor_points: Dict[Vector, Tuple[Vector, Vector, Vector, Vector]] = {}
sensor_lines: Dict[Vector, Tuple[Line, Line, Line, Line]] = {}
for (sensor, beacon) in inp:
mh = sensor.manhattan_distance(beacon)
points = (
Vector(sensor.x, sensor.y - mh),
Vector(sensor.x - mh, sensor.y),
Vector(sensor.x + mh, sensor.y),
Vector(sensor.x, sensor.y + mh),
)
sensor_points[sensor] = points
lines = (
Line.from_points(points[1], points[0]),
Line.from_points(points[3], points[1]),
Line.from_points(points[2], points[0]),
Line.from_points(points[2], points[3]),
)
sensor_lines[sensor] = lines
return sensor_points, sensor_lines
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
inp = parse(instr)
sensor_points, sensor_lines = postprocess(inp)
target_line = 10 if len(inp) == 14 else 2000000
ranges: List[Tuple[int, int]] = []
for (sensor, _) in inp:
points = sensor_points[sensor]
# print("polygon(", (", ".join(map(str, (points[0], points[1], points[3], points[2])))), ")")
lines = sensor_lines[sensor]
if points[0].y <= target_line <= points[3].y:
if target_line == points[1].y:
ranges.append((points[2].x, points[1].x))
elif points[0].y <= target_line < points[1].y:
ranges.append(
(
int((target_line - lines[2].c) / lines[2].m),
int((target_line - lines[0].c) / lines[0].m),
)
)
else:
ranges.append(
(
int((target_line - lines[3].c) / lines[3].m),
int((target_line - lines[1].c) / lines[1].m),
)
)
s: Set[int] = set()
for x in ranges:
s = s.union(set(range(x[1], x[0] + 1)))
for (_, beacon) in inp:
if beacon.y == target_line:
s.discard(beacon.x)
return len(s)
@staticmethod
def two(instr: str) -> int:
inp = parse(instr)
sensor_points, sensor_lines = postprocess(inp)
max_coord = 20 if len(inp) == 14 else 4000000
line_ranges: List[P.Interval] = [P.empty() for _ in range(max_coord + 1)]
for (sensor, _) in inp:
p0, p1, p2, p3 = sensor_points[sensor]
l0, l1, l2, l3 = sensor_lines[sensor]
for y in range(max(p0.y, 0), min(p3.y, max_coord) + 1):
a: int
b: int
if y == sensor.y:
a, b = p1.x, p2.x
elif p0.y <= y < p1.y:
a, b = int((y - l0.c) / l0.m), int((y - l2.c) / l2.m)
else:
a, b = int((y - l1.c) / l1.m), int((y - l3.c) / l3.m)
line_ranges[y] = line_ranges[y] | P.closed(
max(a, 0), min(b, max_coord) + 1
)
for y in range(len(line_ranges)):
res = line_ranges[y]
if not res.atomic:
return (4000000 * res[0].upper) + y
return 0