2018/Day10/Day10A.py

78 lines
1.5 KiB
Python
Raw Permalink Normal View History

2018-12-15 12:25:13 +00:00
import numpy
import fileinput
import re
class Star:
def __init__(self, sx, sy, vx, vy):
self.sx = sx
self.sy = sy
self.vx = vx
self.vy = vy
def __str__(self):
return f"pos: ({self.sx:>2d}, {self.sy:>2d}), vel: ({self.vx:>2d}, {self.vy:>2d})"
def step(self):
self.sx += self.vx
self.sy += self.vy
def step_back(self):
self.sx -= self.vx
self.sy -= self.vy
last_stars = []
stars = []
sxs = []
sys = []
vxs = []
vys = []
count = 0
RE = re.compile("^position=< ?([\d\-]+), ?([\d\-]+)> velocity=< ?([\d\-]+), ?([\d\-]+)>$")
for line in fileinput.input():
match = RE.match(line)
sxs.append(int(match.group(1)))
sys.append(int(match.group(2)))
vxs.append(int(match.group(3)))
vys.append(int(match.group(4)))
count += 1
sxs = numpy.array(sxs)
sys = numpy.array(sys)
vxs = numpy.array(vxs)
vys = numpy.array(vys)
last_dy = 10000000000
steps = 0
while True:
# get the min-y and max-y
min_y = sys.min()
delta_y = sys.max() - min_y
if delta_y > last_dy:
sxs -= vxs
sys -= vys
min_x = sxs.min()
delta_x = sxs.max() - min_x
grid = numpy.zeros((delta_y + 1, delta_x + 1))
grid[sys - min_y, sxs - min_x] = 1
for y in range(delta_y + 1):
for x in range(delta_x + 1):
print('#' if grid[y][x] else '.', end='')
print()
print(steps - 1)
break
last_dy = delta_y
sxs += vxs
sys += vys
steps += 1