-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday20.py
More file actions
97 lines (77 loc) · 2.29 KB
/
Copy pathday20.py
File metadata and controls
97 lines (77 loc) · 2.29 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import math
class Vector3(object):
def __init__(self, l):
self.x = l[0]
self.y = l[1]
self.z = l[2]
def __add__(self, other):
self.x += other.x
self.y += other.y
self.z += other.z
return self
def __repr__(self):
return '<' + str(self.x) + ',' + str(self.y) + ',' + str(self.z) + '>'
def __eq__(self, other):
return self.__dict__ == other.__dict__
class Particle(object):
def __init__(self, id, p, v, a):
self.p = Vector3(p)
self.v = Vector3(v)
self.a = Vector3(a)
self.id = id
def __repr__(self):
return 'p=' + str(self.p) + ', ' + \
'v=' + str(self.v) + ', ' + \
'a=' + str(self.a)
def update(self):
self.v += self.a
self.p += self.v
def dist(v1, v2):
v1 = v1.p
v2 = v2.p
return math.sqrt(math.pow(abs(v1.x - v2.x), 2) +
math.pow(abs(v1.y - v2.y), 2) +
math.pow(abs(v1.z - v2.z), 2))
def removecollisions(particles):
positions = list()
newlistofparticles = list()
todelete = list()
for particle in particles:
if particle.p in positions:
pos = positions.index(particle.p)
if pos not in todelete:
todelete.append(pos)
else:
positions.append(particle.p)
newlistofparticles.append(particle)
for i in range(len(todelete)):
del newlistofparticles[todelete[i] - i]
return newlistofparticles
l = ['p', 'v', 'a']
idx = 0
particles = list()
for i in open("day20.txt").read().split("\n"):
i = i.split(', ')
for x in range(3):
tmp = list(map(int, i[x][3:-1].split(",")))
l[x] = tmp
particles.append(Particle(idx, l[0], l[1], l[2]))
idx += 1
z = [0, 0, 0]
zero = Particle(-1, z, z, z)
minimum = 2 ** 32 - 1
answer = -1
for j in range(40):
for i in particles:
d = dist(i, zero)
if d < minimum:
minimum = d
answer = i.id
i.update()
minimum = 2 ** 32 - 1
particles = removecollisions(particles)
# without removecollisions and with at least 350 iteratioins
# the proper answer for part 1 is 170
print('part1:', answer)
# for part 2 the answer is 571 (40 iterations)
print('part2:', len(particles))