-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_10.py
144 lines (102 loc) · 2.94 KB
/
day_10.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
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
144
import os
import re
from dataclasses import dataclass
from typing import Tuple, List, Optional, Dict
counter = {'point_id': 0}
@dataclass
class Point:
point_id: int
position: Tuple[int, int]
velocity: Tuple[int, int]
@property
def x(self) -> int: return self.position[0]
@property
def y(self) -> int: return self.position[1]
@property
def dx(self) -> int: return self.velocity[0]
@property
def dy(self) -> int: return self.velocity[1]
def step(self, c = 1) -> None:
self.position = self.x + c*self.dx, self.y + c*self.dy
# print(self.position)
def __str__(self): return '#'
def __repr__(self): return '#'
P = Point(point_id=None, position=(3,9), velocity=(1,-2))
def parse_line(line: str) -> Point:
x, y, dx, dy = map(int, re.match(
'position=<([- ]?\d+),\s*([- ]?\d+)> velocity=<([- ]?\d+),\s*([- ]?\d+)>',
line
).groups())
counter['point_id'] += 1
return Point(point_id=counter['point_id'], position=(x, y,), velocity=(dx, dy))
def parse_points(pathname: str = './input/d10.txt') -> List[Point]:
with open(pathname) as fp:
return [parse_line(line.rstrip()) for line in fp]
def distance(p1: Point, p2: Point) -> int:
return abs(p2.y - p1.y) + abs(p2.x - p1.x)
def diameter(ps: List[Point]) -> int:
return max(
distance(p1, p2)
for p1 in ps
for p2 in ps
)
Grid = List[List[Optional[Point]]]
def empty_grid(dim: int) -> Grid:
return [
[None for _ in range(dim)]
for _ in range(dim)
]
def part_one():
points: List[Point] = parse_points()
min_X = min(p.x for p in points)
min_Y = min(p.x for p in points)
for p in points:
p.position = (p.x - min_X, p.y - min_Y)
dim = diameter(points)
print(f'Diameter: {dim}')
grid = {}
for p in points:
grid[p.position] = p
i = 0
for p in points:
p.step(c=10519)
min_X = min(p.x for p in points)
min_Y = min(p.x for p in points)
for p in points:
p.position = (p.x - min_X, p.y - min_Y)
dim = diameter(points)
print(f'Diameter: {dim}')
while i < 10**4:
D = diameter(points)
print(i, D)
for p in points:
p.step()
if D < 80:
min_X = min(p.x for p in points)
min_Y = min(p.x for p in points)
for p in points:
p.position = (p.x - min_X, p.y - min_Y)
print('woot')
draw(points, D)
break
i += 1
print('wtf')
def draw(points: List[Point], dim: int) -> None:
G = empty_grid(dim)
for p in points:
G[p.y][p.x] = '#'
for p in points:
print(p.velocity)
G = [
[
'.' if G[row][col] is None else '#'
for col in range(dim)
]
for row in range(dim)
]
print()
for x in G:
print(' '.join(x))
print()
if __name__ == '__main__':
part_one()