This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
particle_swarm_part_two.py
68 lines (56 loc) · 2.18 KB
/
particle_swarm_part_two.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
"""
--- Part Two ---
To simplify the problem further, the GPU would like to remove any particles that
collide. Particles collide if their positions ever exactly match. Because
particles are updated simultaneously, more than two particles can collide at the
same time and place. Once particles collide, they are removed and cannot collide
with anything else after that tick.
For example:
p=<-6,0,0>, v=< 3,0,0>, a=< 0,0,0>
p=<-4,0,0>, v=< 2,0,0>, a=< 0,0,0> -6 -5 -4 -3 -2 -1 0 1 2 3
p=<-2,0,0>, v=< 1,0,0>, a=< 0,0,0> (0) (1) (2) (3)
p=< 3,0,0>, v=<-1,0,0>, a=< 0,0,0>
p=<-3,0,0>, v=< 3,0,0>, a=< 0,0,0>
p=<-2,0,0>, v=< 2,0,0>, a=< 0,0,0> -6 -5 -4 -3 -2 -1 0 1 2 3
p=<-1,0,0>, v=< 1,0,0>, a=< 0,0,0> (0)(1)(2) (3)
p=< 2,0,0>, v=<-1,0,0>, a=< 0,0,0>
p=< 0,0,0>, v=< 3,0,0>, a=< 0,0,0>
p=< 0,0,0>, v=< 2,0,0>, a=< 0,0,0> -6 -5 -4 -3 -2 -1 0 1 2 3
p=< 0,0,0>, v=< 1,0,0>, a=< 0,0,0> X (3)
p=< 1,0,0>, v=<-1,0,0>, a=< 0,0,0>
------destroyed by collision------
------destroyed by collision------ -6 -5 -4 -3 -2 -1 0 1 2 3
------destroyed by collision------ (3)
p=< 0,0,0>, v=<-1,0,0>, a=< 0,0,0>
In this example, particles 0, 1, and 2 are simultaneously destroyed at the time
and place marked X. On the next tick, particle 3 passes through unharmed.
How many particles are left after all collisions are resolved?
"""
from utils.parse import parse
p = parse()
collided = []
tick = 0
while True:
occupied = {}
if tick > 1000:
print(len(p) - len(collided))
break
for i in range(0, len(p)):
if p[i]['x']:
continue
p[i]['v'][0] += p[i]['a'][0]
p[i]['v'][1] += p[i]['a'][1]
p[i]['v'][2] += p[i]['a'][2]
p[i]['p'][0] += p[i]['v'][0]
p[i]['p'][1] += p[i]['v'][1]
p[i]['p'][2] += p[i]['v'][2]
pos = ''.join([str(val) for val in p[i]['p']])
if pos not in occupied.keys():
occupied[pos] = []
occupied[pos].append(p[i]['id'])
for pos, ids in occupied.items():
if len(ids) > 1:
for i in ids:
p[i]['x'] = True
collided.append(id)
tick += 1