-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday12_moons.py
234 lines (175 loc) · 6.55 KB
/
day12_moons.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from collections import defaultdict
import functools
import itertools
import math
import re
from typing import Dict, List, NamedTuple
import pytest
POSITIONS = r"<x=(?P<x>-*\d+), y=(?P<y>-*\d+), z=(?P<z>-*\d+)>"
def lcm(denominators):
"""https://stackoverflow.com/a/49816058/4326704"""
return functools.reduce(lambda a,b: a*b // math.gcd(a,b), denominators)
class Position(NamedTuple):
x: int
y: int
z: int
def __add__(self, other):
return Position(x=self.x + other.x, y=self.y + other.y, z=self.z + other.z)
def __sub__(self, other):
return Position(x=self.x - other.x, y=self.y - other.y, z=self.z - other.z)
def __iadd_(self, other):
return self + other
def __isub__(self, other):
return self - other
def __mul__(self, other):
if isinstance(other, int):
result = []
for value in list(self):
result.append(value * other)
return Position(*result)
raise NotImplementedError("unreachable")
def __rmul__(self, other):
return self * other
def motion_due_to_gravity(self, other):
"""Compare two planets, find out how they move with respect to each other"""
result = []
for value, other_value in zip(list(self), list(other)):
if value == other_value:
result.append(0)
elif value > other_value:
result.append(-1)
elif value < other_value:
result.append(1)
else:
raise ValueError("unrreachable")
return Vector(*result)
Vector = Position # TODO make this an actual different type
class Moon(NamedTuple):
position: Position
velocity: Vector
class State:
def __init__(self):
self._state = {}
def __getitem__(self, key):
return self._state[key]
def __setitem__(self, key, value):
self._state[key] = value
def __iter__(self):
return iter(self._state)
def __repr__(self):
return repr(self._state)
def items(self):
return self._state.items()
def keys(self):
return self._state.keys()
@classmethod
def from_moons(cls, moons: List[Moon]):
self = cls()
for index, moon_details in enumerate(moons):
self[index] = moon_details
return self
def load_positions(lines: List[str]) -> List[Moon]:
"""TODO abstract this into a generator"""
p = re.compile(POSITIONS)
moons = []
for idx, line in enumerate(lines):
cleaned_line = line.strip()
m = p.match(cleaned_line)
pos = Position(x=int(m.group("x")), y=int(m.group("y")), z=int(m.group("z")))
vel = Vector(x=0, y=0, z=0)
moons.append(Moon(position=pos, velocity=vel))
return moons
def simulate(initial_state: State, num_timesteps: int) -> State:
"""Given initial state, increment by timesteps, return state"""
step = 0
current_state = initial_state
while True:
motion_tracker = {key: Position(x=0, y=0, z=0) for key in current_state.keys()}
for m1, m2 in itertools.combinations(current_state.keys(), r=2):
moon1 = current_state[m1].position
moon2 = current_state[m2].position
gravity_delta = moon1.motion_due_to_gravity(moon2)
motion_tracker[m1] += gravity_delta
motion_tracker[m2] -= gravity_delta
for key in motion_tracker.keys():
motion_tracker[key] += current_state[key].velocity
for key in current_state.keys():
new_velocity = motion_tracker[key] * 1 # creates a copy
new_position = current_state[key].position + motion_tracker[key]
current_state[key] = Moon(position=new_position, velocity=new_velocity)
step += 1
if step >= num_timesteps:
break
return current_state
def calculate_energy(current_state: State) -> int:
total_energy = 0
for key, state_details in current_state.items():
potential_energy = sum(abs(val) for val in list(state_details.position))
kinect_energy = sum(abs(val) for val in list(state_details.velocity))
total_energy += potential_energy * kinect_energy
return total_energy
def find_orbit_pattern(initial_state: State):
current_state = initial_state
timestep = 0
seen_xs = set()
seen_ys = set()
seen_zs = set()
while True:
timestep += 1
current_state = simulate(current_state, num_timesteps=1)
x = []
y = []
z = []
for key in current_state.keys():
pos = current_state[key].position
vel = current_state[key].velocity
x.extend([key, pos.x, vel.x])
y.extend([key, pos.y, vel.y])
z.extend([key, pos.z, vel.z])
x = tuple(x)
y = tuple(y)
z = tuple(z)
if x in seen_xs and y in seen_ys and z in seen_zs:
return (len(seen_xs), len(seen_ys), len(seen_zs))
seen_xs.add(x)
seen_ys.add(y)
seen_zs.add(z)
TEST_INPUT1 = """<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>"""
TEST_INPUT2 = """<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>"""
@pytest.mark.parametrize(
"positions, timesteps, expected_total_energy",
[(TEST_INPUT1, 10, 179), (TEST_INPUT2, 100, 1940)],
)
def test_calculate_total_energy(positions, timesteps, expected_total_energy):
moons = load_positions(positions.split("\n"))
initial_state = State.from_moons(moons)
final_state = simulate(initial_state, num_timesteps=timesteps)
assert calculate_energy(final_state) == expected_total_energy
@pytest.mark.parametrize(
"positions, expected_timesteps", [(TEST_INPUT2, 4686774924)]
)
def test_timesteps_before_repeat(positions, expected_timesteps):
moons = load_positions(positions.split("\n"))
initial_state = State.from_moons(moons)
periodic_orbit = find_orbit_pattern(initial_state)
assert lcm(periodic_orbit) == expected_timesteps
if __name__ == "__main__":
lines = []
with open("2019/data/day12_input.txt", "r") as f:
for line in f.readlines():
lines.append(line.strip())
moons = load_positions(lines)
initial_state = State.from_moons(moons)
final_state = simulate(initial_state, num_timesteps=1000)
energy = calculate_energy(final_state)
print(f"Total energy in the system is: {energy}")
initial_state = State.from_moons(moons)
periodic_orbit = find_orbit_pattern(initial_state)
timesteps = lcm(periodic_orbit)
print(f"Num timesteps before repeating is: {timesteps}")