-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathd6.py
61 lines (47 loc) · 1.31 KB
/
d6.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
from dataclasses import dataclass
from collections import Counter
points = []
@dataclass
class Point:
id: int
x: int
y: int
def dist_to(self, x, y):
return abs(self.x - x) + abs(self.y - y)
@dataclass
class Neighbour:
distance: int
to: int
with open('input') as f:
for i, line in enumerate(f):
coords = tuple(map(int, line.split(", ")))
points.append(Point(i, coords[0], coords[1]))
maxx = max(point.x for point in points)
maxy = max(point.y for point in points)
# p1
matrix = []
for y in range(maxy):
row = []
for x in range(maxx):
min_dist = maxx + maxy
best = None
for point in points:
dist = point.dist_to(x, y)
if dist < min_dist:
min_dist = dist
best = point
row.append(Neighbour(min_dist, best))
matrix.append(row)
infinite = set(n.to.id for n in matrix[0]) | set(
n.to.id for n in matrix[-1]) | set(y[0].to.id for y in matrix) | set(y[-1].to.id for y in matrix)
counter = Counter(
n.to.id for y in matrix for n in y if n.to.id not in infinite)
print(max(counter.values()))
# p2
blocks = 0
for y in range(maxy):
for x in range(maxx):
total_dist = sum(p.dist_to(x, y) for p in points)
if total_dist < 10000:
blocks += 1
print(blocks)