-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday12.py
executable file
·148 lines (112 loc) · 2.36 KB
/
day12.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
from utils.all import *
# fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
moons = [
[-7, 17, -11],
[9, 12, 5],
[-9, 0, -4],
[4, 6, 0]
]
vels = [
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]
]
for _ in range(1000):
accels = [
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]
]
for i in range(4):
for j in range(4):
if i == j:
continue
if moons[i][0] < moons[j][0]:
accels[i][0] += 1
accels[j][0] -= 1
if moons[i][1] < moons[j][1]:
accels[i][1] += 1
accels[j][1] -= 1
if moons[i][2] < moons[j][2]:
accels[i][2] += 1
accels[j][2] -= 1
# for i, (m, v, a) in enumerate(zip(moons, vels, accels)):
# print('{:10d} {:10d} {:10d} | {:10d} {:10d} {:10d} | {:10d} {:10d} {:10d}'.format(*m, *v, *a))
# print('-------------------'*6)
for i in range(4):
vels[i][0] += accels[i][0]
vels[i][1] += accels[i][1]
vels[i][2] += accels[i][2]
moons[i][0] += vels[i][0]
moons[i][1] += vels[i][1]
moons[i][2] += vels[i][2]
pot = [sum(map(abs, m)) for m in moons]
kin = [sum(map(abs, m)) for m in vels]
tot = sum(p * k for p, k in zip(pot, kin))
advent.submit_answer(1, tot)
moons = [
[-7, 17, -11],
[9, 12, 5],
[-9, 0, -4],
[4, 6, 0]
]
vels = [
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]
]
history = [set() for _ in range(3)]
periods = [0] * 3
found = [False] * 3
steps = 0
while not all(found):
# if steps % 10000 == 0:
# print(steps, periods)
for dim in range(3):
x = tuple((m[dim], v[dim]) for m, v in zip(moons, vels))
if not found[dim] and x in history[dim]:
periods[dim] = steps
found[dim] = True
else:
history[dim].add(x)
accels = [
[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]
]
for i in range(4):
for j in range(4):
if i == j:
continue
if moons[i][0] < moons[j][0]:
accels[i][0] += 1
accels[j][0] -= 1
if moons[i][1] < moons[j][1]:
accels[i][1] += 1
accels[j][1] -= 1
if moons[i][2] < moons[j][2]:
accels[i][2] += 1
accels[j][2] -= 1
for i in range(4):
vels[i][0] += accels[i][0]
vels[i][1] += accels[i][1]
vels[i][2] += accels[i][2]
moons[i][0] += vels[i][0]
moons[i][1] += vels[i][1]
moons[i][2] += vels[i][2]
steps += 1
# print(periods)
from math import gcd
lcm = periods[0]
for i in periods[1:]:
lcm = lcm*i//gcd(lcm, i)
# 2988031200 not right
advent.submit_answer(2, lcm)