/
day10.py
64 lines (48 loc) · 1.34 KB
/
day10.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
from dataclasses import dataclass
import re
import sys
import time
from PIL import Image, ImageDraw
@dataclass
class Star:
x: int
y: int
xspeed: int
yspeed: int
starmap = []
for line in open('day10.txt'):
m = re.match(
r"position=<([ -]+\d+), ([ -]+\d+)> velocity=<([ -]+\d+), ([ -]+\d+)>", line)
starmap.append(Star(int(m[1]), int(m[2]), int(m[3]), int(m[4])))
acc = 100
prevCirc = sys.maxsize
step = 0
found = False
while True:
for star in starmap:
star.x += star.xspeed * acc
star.y += star.yspeed * acc
step += acc
maxx = max(starmap, key=lambda x: x.x).x
maxy = max(starmap, key=lambda x: x.y).y
minx = min(starmap, key=lambda x: x.x).x
miny = min(starmap, key=lambda x: x.y).y
circ = (maxx - minx) + (maxy - miny)
width = maxy - miny
# slow down, now that we could render this image
if (maxx < 500):
acc = 1
if found:
print("Solution: ", step)
# show the image
img = Image.new('RGB', (maxx-minx+6, maxy-miny+6), (255, 255, 255))
draw = ImageDraw.Draw(img)
for star in starmap:
draw.point((star.x - minx+3, star.y-miny+3), (255, 0, 0))
img.show()
break
# we're growing again, go back one step
if prevCirc < circ:
acc = -1
found = True
prevCirc = circ