-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.py
57 lines (45 loc) · 1.48 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
from lib import *
input = read_input(2019, 12)
moons = []
for line in input.splitlines():
x, y, z = map(int, re.match(r"^<x=([\-\d]+), y=([\-\d]+), z=([\-\d]+)>$", line).groups())
moons.append(([x, y, z], [0, 0, 0]))
for _ in range(1000):
for pos, vel in moons:
for p2, _ in moons:
for i in range(3):
if pos[i] < p2[i]:
vel[i] += 1
elif pos[i] > p2[i]:
vel[i] -= 1
for pos, vel in moons:
for i in range(3):
pos[i] += vel[i]
print(sum(sum(map(abs, pos)) * sum(map(abs, vel)) for pos, vel in moons))
moons = []
for line in input.splitlines():
x, y, z = map(int, re.match(r"^<x=([\-\d]+), y=([\-\d]+), z=([\-\d]+)>$", line).groups())
moons.append(([x, y, z], [0, 0, 0]))
def make_state(dim):
return tuple(e[dim] for moon in moons for e in moon)
def simulate_dimension(dim):
history = {}
time = 0
while True:
state = make_state(dim)
if state in history:
return time - history[state]
history[state] = time
time += 1
for pos, vel in moons:
for p2, _ in moons:
if pos[dim] < p2[dim]:
vel[dim] += 1
elif pos[dim] > p2[dim]:
vel[dim] -= 1
for pos, vel in moons:
pos[dim] += vel[dim]
x, y, z = map(simulate_dimension, range(3))
out = x * y // math.gcd(x, y)
out = out * z // math.gcd(out, z)
print(out)