2018 Day 10 numpy cleanup

This commit is contained in:
Luke Hubmayer-Werner 2018-12-10 20:12:38 +10:30
parent 61bccd5e8f
commit 6ee1e60a84
1 changed files with 12 additions and 18 deletions

View File

@ -1,27 +1,21 @@
with open('day10-input', 'r') as file: with open('day10-input', 'r') as file:
data = [l.strip('\n') for l in file] data = [l.strip('\n') for l in file]
import numpy as np import numpy as np
import re
positions = [] numbers = np.array([[int(s) for s in re.findall(r'-?\d+', d)] for d in data], dtype=np.int64)
velocities = [] start_positions = numbers[:,:2]
velocities = numbers[:,2:]
for line in data:
pos = [int(line[10:16]), -int(line[18:24])] # Invert Y for graphing purposes
vel = [int(line[36:38]), -int(line[39:42])] # Invert Y for graphing purposes
positions.append(pos)
velocities.append(vel)
max_t = 11000 max_t = 11000
curr_pos = np.vstack(positions) positions = np.empty((*start_positions.shape, max_t))
vels = np.vstack(velocities)
positions = np.empty((385,2,max_t))
heights = np.empty(max_t, dtype=np.int64)
for t in range(max_t): for t in range(max_t):
positions[:,:,t] = curr_pos + t*vels positions[:,:,t] = start_positions + t*velocities
heights[t] = positions[:,1,t].max() - positions[:,1,t].min()
heights = positions[:,1,:].max(0)-positions[:,1,:].min(0)
t = np.argmin(heights) t = np.argmin(heights)
from pyqtgraph.Qt import QtCore, QtGui from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg import pyqtgraph as pg
@ -31,10 +25,10 @@ win.resize(2000,600)
win.setWindowTitle('Scatter Plot') win.setWindowTitle('Scatter Plot')
win.show() win.show()
p5 = win.addPlot(title=f't={t}') plot = win.addPlot(title=f't={t}')
p5.plot(positions[:,:,t], pen=None, symbol='s', symbolPen=None, symbolSize=10, symbolBrush=(100, 100, 255, 250)) plot.plot(positions[:,:,t], pen=None, symbol='s', symbolPen=None, symbolSize=10, symbolBrush=(100, 100, 255, 250))
plot.getViewBox().invertY(True)
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):