-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.py
69 lines (56 loc) · 1.81 KB
/
12.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
from utils import read_file
import itertools
class Moon():
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
self.dx = 0
self.dy = 0
self.dz = 0
self.count = 0
def __repr__(self):
return "Moon(step=%d, pos=(%d, %d, %d) spd=(%d, %d, %d))" % (self.count,
self.x,
self.y,
self.z,
self.dx,
self.dy,
self.dz)
def step(self):
self.x += self.dx
self.y += self.dy
self.z += self.dz
self.count += 1
def update_gravity(self, other):
if self.x > other.x:
self.dx -= 1
elif self.x < other.x:
self.dx += 1
if self.y > other.y:
self.dy -= 1
elif self.y < other.y:
self.dy += 1
if self.z > other.z:
self.dz -= 1
elif self.z < other.z:
self.dz += 1
def energy(self):
return (abs(self.x) + abs(self.y) + abs(self.z)) * (abs(self.dx) + abs(self.dy) + abs(self.dz))
print("#--- part1 ---#")
moons = [
Moon(19, -10, 7),
Moon(1, 2, -3),
Moon(14, -4, 1),
Moon(8, 7, -6)
]
for _ in range(1000):
moonlist = moons.copy()
while len(moonlist) > 0:
for moon in moonlist[1:]:
moonlist[0].update_gravity(moon)
moon.update_gravity(moonlist[0])
moonlist.pop(0)
for moon in moons:
moon.step()
print(sum(m.energy() for m in moons))