-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.py
147 lines (119 loc) · 2.7 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
data = """<x=-1, y=7, z=3>
<x=12, y=2, z=-13>
<x=14, y=18, z=-8>
<x=17, y=4, z=-4>""".splitlines()
from itertools import combinations
import re
pattern = re.compile(r"<x=(\-?\d+), y=(\-?\d+), z=(\-?\d+)>")
moons = [
[
[
int((match := pattern.match(i)).group(1)),
int(match.group(2)),
int(match.group(3)),
],
[0, 0, 0],
]
for i in data
]
x_states = set()
y_states = set()
z_states = set()
x_loop = -1
y_loop = -1
z_loop = -1
step = 0
x_state = (
moons[0][0][0],
moons[1][0][0],
moons[2][0][0],
moons[0][1][0],
moons[1][1][0],
moons[2][1][0],
)
y_state = (
moons[0][0][1],
moons[1][0][1],
moons[2][0][1],
moons[0][1][1],
moons[1][1][1],
moons[2][1][1],
)
z_state = (
moons[0][0][2],
moons[1][0][2],
moons[2][0][2],
moons[0][1][2],
moons[1][1][2],
moons[2][1][2],
)
while -1 in [x_loop, y_loop, z_loop]:
if x_state in x_states and x_loop == -1:
x_loop = step
else:
x_states.add(x_state)
if y_state in y_states and y_loop == -1:
y_loop = step
else:
y_states.add(y_state)
if z_state in z_states and z_loop == -1:
z_loop = step
else:
z_states.add(z_state)
for moon, moon2 in combinations(moons, 2):
x1, y1, z1 = moon[0]
x2, y2, z2 = moon2[0]
if x1 > x2:
moon[1][0] -= 1
moon2[1][0] += 1
if x1 < x2:
moon[1][0] += 1
moon2[1][0] -= 1
if y1 > y2:
moon[1][1] -= 1
moon2[1][1] += 1
if y1 < y2:
moon[1][1] += 1
moon2[1][1] -= 1
if z1 > z2:
moon[1][2] -= 1
moon2[1][2] += 1
if z1 < z2:
moon[1][2] += 1
moon2[1][2] -= 1
for moon in moons:
moon[0][0] += moon[1][0]
moon[0][1] += moon[1][1]
moon[0][2] += moon[1][2]
step += 1
if step == 1000:
print(sum(sum(abs(i) for i in j[0]) * sum(abs(i) for i in j[1]) for j in moons))
x_state = (
moons[0][0][0],
moons[1][0][0],
moons[2][0][0],
moons[0][1][0],
moons[1][1][0],
moons[2][1][0],
)
y_state = (
moons[0][0][1],
moons[1][0][1],
moons[2][0][1],
moons[0][1][1],
moons[1][1][1],
moons[2][1][1],
)
z_state = (
moons[0][0][2],
moons[1][0][2],
moons[2][0][2],
moons[0][1][2],
moons[1][1][2],
moons[2][1][2],
)
from math import gcd
def lcm(a, b):
"""Compute the lowest common multiple of a and b"""
return a * b // gcd(a, b)
print(lcm(lcm(x_loop, y_loop), z_loop))