-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
216 lines (157 loc) · 6.07 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
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
import argparse
import math
from functools import reduce
parser = argparse.ArgumentParser(description="AoC day 2")
parser.add_argument("file", help="The file that should be sourced")
parser.add_argument("-p", "--phase", help="The part of the exercise that we are at", type=int, default=1)
parser.add_argument("-t", "--target", help="A target value being aimed for", type=int, default=0)
def main(argv):
print(f"test, {argv}")
infile = open(argv.file, "r")
allItems = []
for line in infile:
# do a line operation
if line:
allItems.append(line)
infile.close()
# commands require a little more processing
commands = []
for item in allItems:
commands.append({})
item = item.strip('<>\n')
numSet = str.split(item, ',')
for block in numSet:
block = block.split('=')
block2 = list(map(lambda x: x.strip(' \n'), block))
commands[-1][block2[0]] = block2[1]
moons = []
for c in commands:
moons.append(Moon(int(c["x"]), int(c["y"]), int(c["z"])))
if argv.phase == 1:
sol = solutionPt1(moons, argv.target)
print(f"The total energy is {sol}")
elif argv.phase == 2:
sol = solutionPt2(moons, argv.target)
print(f"The repeat is at {sol}")
class Moon:
id = 0
def __init__(self, x, y, z):
self.id = Moon.id
self.pos = x, y, z
self.vel = 0, 0, 0
self.vel1 = 0
self.energy = 0
self.checked_with = []
Moon.id += 1
def add_gravity(self, other_moon):
change = [0, 0, 0]
for iter in range(0, 3):
if self.pos[iter] > other_moon.pos[iter]:
change[iter] = -1
elif self.pos[iter] < other_moon.pos[iter]:
change[iter] = 1
self.vel = self.vel[0] + change[0], self.vel[1] + change[1], self.vel[2] + change[2]
other_moon.vel = other_moon.vel[0] - change[0], other_moon.vel[1] - change[1], other_moon.vel[2] - change[2]
self.checked_with.append(other_moon.id)
other_moon.checked_with.append(self.id)
def s_add_gravity(self, other_moon):
change = [0]
if self.pos[0] > other_moon.pos[0]:
change[0] = -1
elif self.pos[0] < other_moon.pos[0]:
change[0] = 1
self.vel = (self.vel[0] + change[0]),
other_moon.vel = (other_moon.vel[0] - change[0]),
self.checked_with.append(other_moon.id)
other_moon.checked_with.append(self.id)
def reset_checked_by(self):
self.checked_with = []
def apply_velocity(self):
self.pos = self.vel[0] + self.pos[0], self.vel[1] + self.pos[1], self.vel[2] + self.pos[2]
def s_apply_velocity(self):
self.pos = (self.vel[0] + self.pos[0]),
def find_energy(self):
potential = 0
for i in self.pos:
potential += abs(i)
kinetic = 0
for i in self.vel:
kinetic += abs(i)
self.energy = kinetic * potential
return self.energy
def __str__(self):
return f"<pos id={self.id} x={self.pos[0]}, y={self.pos[1]}, z={self.pos[2]}>" \
f"<vel x={self.vel[0]}, y={self.vel[1]}, z={self.vel[2]}>"
def __eq__(self, other):
return self.pos == other.pos and self.vel == other.vel
# To apply gravity, consider every pair of moons. On each axis (x, y, and z),
# the velocity of each moon changes by exactly +1 or -1 to pull the moons together.
# For example, if Ganymede has an x position of 3, and Callisto has a x position of 5,
# then Ganymede's x velocity changes by +1 (because 5 > 3) and
# Callisto's x velocity changes by -1 (because 3 < 5).
# However, if the positions on a given axis are the same,
# the velocity on that axis does not change for that pair of moons.
# Once all gravity has been applied, apply velocity:
# simply add the velocity of each moon to its own position.
#Then, it might help to calculate the total energy in the system.
# The total energy for a single moon is its potential energy multiplied by its kinetic energy.
# A moon's potential energy is the sum of the absolute values of its x, y, and z position coordinates.
# A moon's kinetic energy is the sum of the absolute values of its velocity coordinates.
def solutionPt1(moons, target_cycle):
cycle = 0
while cycle < target_cycle:
for m in moons:
for m2 in moons:
if m.id not in m2.checked_with and not m.id == m2.id:
m.add_gravity(m2)
for m in moons:
m.apply_velocity()
m.reset_checked_by()
cycle += 1
energy = sum(m.find_energy() for m in moons)
for p in moons:
print(p)
return energy
def lcm(a, b):
return (a * b) // math.gcd(a, b)
def solutionPt2(moons, target_cycle):
POS = 0
VEL = 1
cycles = []
dimm = 0
while dimm < 3:
repeated = False
cycle = 0
initial = []
now = []
for m in moons:
initial.append((m.pos[dimm], 0))
now.append((m.pos[dimm], 0))
while not repeated:
# while cycle < target_cycle:
# grav
for i in range(0, len(initial)):
for j in range(0, len(initial)):
if i != j:
if now[i][POS] > now[j][POS]:
now[i] = now[i][POS], now[i][VEL]-1
if now[i][POS] < now[j][POS]:
now[i] = now[i][POS], now[i][VEL]+1
# vel
for i in range(0, len(initial)):
now[i] = now[i][VEL] + now[i][POS], now[i][VEL]
repeated = True
for i in range(0, len(initial)):
repeated = repeated and now[i] == initial[i]
cycle += 1
print(f"Dimm {dimm} Cycle {cycle}")
dimm += 1
cycles.append(cycle)
overlap = 1
gcd = math.gcd(cycles[0], cycles[1])
gcd = math.gcd(gcd, cycles[2])
print(f"answer: {reduce(lcm, cycles)}")
for c in cycles:
overlap *= c/gcd
return overlap
main(parser.parse_args())