Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solution to Day 20: Particle Swarm
#aoc_d20.py
inp = open('input_d20.txt').readlines()
'''inp = ["p=<3,0,0>, v=<2,0,0>, a=<-1,0,0>",
"p=<4,0,0>, v=<0,0,0>, a=<-2,0,0>"]
'''
import re
data = {}
idx = 0
for i in inp:
#print i
m = re.match("p=<(-?\d+),(-?\d+),(-?\d+)>,\sv=<(-?\d+),(-?\d+),(-?\d+)>,\sa=<(-?\d+),(-?\d+),(-?\d+)>",i)
data[idx] = {'px':int(m.group(1)),'py':int(m.group(2)),'pz':int(m.group(3)),
'vx':int(m.group(4)),'vy':int(m.group(5)),'vz':int(m.group(6)),
'ax':int(m.group(7)),'ay':int(m.group(8)),'az':int(m.group(9))}
idx += 1
distances = {}
#for i in range(len(data)):
# disances[i] = 1000000
#min_distance = 1000000
r = 0
br = int(input('Podaj breaker: '))
while True:
if r > br:
break
r += 1
#print 'next'
for k,v in data.items():
v['vx'] += v['ax']
v['vy'] += v['ay']
v['vz'] += v['az']
v['px'] += v['vx']
v['py'] += v['vy']
v['pz'] += v['vz']
#print k,v
for k,v in data.items():
dist = abs(v['px'])+abs(v['py'])+abs(v['pz'])
distances[k] = dist
#print disances
min_d = min(distances.items(), key=lambda x: x[1])
print 'Min:', min_d
#print disances
#print min_dist, min_distance
#print min_dist < min_distance
min_distance = min_d[1]
print 'Min dist: ',min_distance,' Min idx: ',min_d[0]
#print distances
#print data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment