-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06.py
More file actions
71 lines (56 loc) · 1.68 KB
/
06.py
File metadata and controls
71 lines (56 loc) · 1.68 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
from utils import (
read_file,
manhattan_distance
)
import numpy as np
max_x, max_y = 0, 0
points = []
for row in read_file('06.txt'):
x, y = row.split(", ")
points.append((int(x), int(y)))
if int(x) > max_x:
max_x = int(x)
if int(y) > max_y:
max_y = int(y)
def part1():
def closest_point(x, y, points):
dist = 1000000000000
point = -1
for i in range(len(points)):
dist_ = manhattan_distance((x, y), points[i])
if dist_ < dist:
dist = dist_
point = i
elif dist_ == dist:
point = -1
return point
area = {}
# genereate a grid of all the closes pont id:s
grid = np.zeros((max_x + 1, max_y + 1), dtype=np.int)
for y in range(max_y + 1):
for x in range(max_x + 1):
grid[x, y] = closest_point(x, y, points)
area[grid[x, y]] = area.get(grid[x, y], 0) + 1
# delete all border points
for x in range(max_x + 1):
area.pop(grid[x, 0], None)
area.pop(grid[x, max_y], None)
for y in range(max_y + 1):
area.pop(grid[0, y], None)
area.pop(grid[max_x, y], None)
return max(area.values())
def part2():
def sum_of_distances(x, y, points):
dist = 0
for point in points:
dist += manhattan_distance((x, y), point)
return dist
grid = np.zeros((max_x + 1, max_y + 1), dtype=np.int)
for y in range(max_y + 1):
for x in range(max_x + 1):
grid[x, y] = sum_of_distances(x, y, points)
return len(np.argwhere(grid < 10000))
print("#--- part1 ---#")
print(part1())
print("#--- part2 ---#")
print(part2())