re: AoC Day 10: The Stars Align VIEW POST

my solution here 😃 thanks for your suggestions

it's coded in Python. it uses vectorization with numpy ndarrays

positions_nda += velocities_nda * iterations

In order to find the instant for points of light alignment, it uses minimize from scipy.optimize module,

from scipy.optimize import minimize
import numpy as np
from parse import parse

filename = 'input'
filepath = f'data/{filename}.plain'

positions = []
velocities = []

pattern = 'position=<{px},{py}> velocity=<{vx},{vy}>'

for line in open(filepath):
result = parse(pattern, line)
position = (int(result['px']), int(result['py']))
positions.append(position)
velocity = (int(result['vx']), int(result['vy']))
velocities.append(velocity)

positions_nda = np.array(positions)
velocities_nda = np.array(velocities)

def gen_area(iterations, initial_positions, velocities):
positions = initial_positions + velocities * i
x_min, y_min = positions.min(axis=0)
x_max, y_max = positions.max(axis=0)
w = x_max - x_min
h = y_max - y_min
area = w * h
return area

# identify the second that minimizes the dispersion of points
t = minimize(gen_area, 0, (positions_nda, velocities_nda))
second = int(np.round(t.x))

positions_nda += velocities_nda * second

def draw(positions_nda):

x_min, y_min = positions_nda.min(axis=0)
x_max, y_max = positions_nda.max(axis=0)

for y in range(y_min, y_max + 1):
line = ''
for x in range(x_min, x_max + 1):
if [x, y] in positions_nda.tolist():
line += '#'
else:
line += '.'
print(line)

draw(positions_nda)
code of conduct - report abuse  