-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
96 lines (73 loc) · 2.01 KB
/
day20.py
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
from collections import deque
from adventlib import *
DAY = 20
class Vec(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __iadd__(self, other):
self.x += other.x
self.y += other.y
self.z += other.z
return self
def dist(self):
return abs(self.x) + abs(self.y) + abs(self.z)
def tup(self):
return self.x, self.y, self.z
def ss(a, b):
return a.x * b.x >= 0 and a.y * b.y >= 0 and a.z * b.z >= 0
class Particle(object):
def __init__(self, id, pos, vel, acc):
self.id = id
self.pos = Vec(*pos)
self.vel = Vec(*vel)
self.acc = Vec(*acc)
def dist(self):
return self.pos.dist()
def upd(self):
self.vel += self.acc
self.pos += self.vel
def ign(self):
return ss(self.vel, self.acc) and ss(self.pos, self.vel)
def main():
inp = store_input(DAY)
if inp is None:
return
lines = list(split_lines(inp))
parts = []
for pi, l in enumerate(lines):
p, v, a = l.split(", ")
ps = [int(i) for i in p[3:-1].split(",")]
vs = [int(i) for i in v[3:-1].split(",")]
cs = [int(i) for i in a[3:-1].split(",")]
parts.append(Particle(pi, ps, vs, cs))
closest = parts[0].id
dist = parts[0].dist()
parts = deque(parts)
for i in range(2000):
if i % 100 == 0:
print(i)
nparts = deque([])
colld = {}
closest = parts[0].id
dist = parts[0].dist()
for p in parts:
if p.id is None:
continue
pd = p.dist()
if pd < dist:
dist = pd
closest = p.id
p.upd()
t = p.pos.tup()
c = colld.get(t, None)
if c is not None:
c.id = None
else:
colld[t] = p
nparts.append(p)
parts = nparts
print(len(parts))
if __name__ == '__main__':
main()