/
D12.py
218 lines (184 loc) · 5.46 KB
/
D12.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
import re
import sys
import math
from itertools import permutations
from OpenGL.GL import (
glClear,
GL_COLOR_BUFFER_BIT,
GL_DEPTH_BUFFER_BIT,
glPushMatrix,
glMaterialfv,
GL_DIFFUSE,
glTranslatef,
glPopMatrix,
GL_FRONT,
glClearColor,
glShadeModel,
GL_SMOOTH,
GL_CULL_FACE,
glEnable,
GL_DEPTH_TEST,
GL_LIGHTING,
glLightfv,
GL_LIGHT0,
GL_POSITION,
glLightf,
GL_CONSTANT_ATTENUATION,
GL_LINEAR_ATTENUATION,
glMatrixMode,
GL_PROJECTION,
GL_MODELVIEW,
)
from OpenGL.GLU import gluPerspective, gluLookAt
from OpenGL.GLUT import (
glutSolidSphere,
glutPostRedisplay,
glutDisplayFunc,
glutSwapBuffers,
glutTimerFunc,
glutInit,
glutInitDisplayMode,
GLUT_DOUBLE,
GLUT_RGB,
GLUT_DEPTH,
glutInitWindowSize,
glutInitWindowPosition,
glutCreateWindow,
glutMainLoop
)
with open('input/d12.txt') as f:
d12_input = [tuple(int(p) for p in re.search(
r'<\w=(-?\d+), \w=(-?\d+), \w=(-?\d+)>',
l.replace('\n', '')).groups()) for l in f.readlines()]
class Moon:
def __init__(self, pos):
self.x, self.y, self.z = pos
self.vx, self.vy, self.vz = (0, 0, 0)
self.sphere = None
@property
def kinetic_energy(self):
return abs(self.x) + abs(self.y) + abs(self.z)
@property
def potential_energy(self):
return abs(self.vx) + abs(self.vy) + abs(self.vz)
@property
def stringify(self):
return '|'.join([
str(self.x),
str(self.y),
str(self.z),
str(self.vx),
str(self.vy),
str(self.vz)
])
def __repr__(self):
return 'pos=<x= %s, y= %s, z=%s>, vel=<x=%s, y=%s, z= %s>' % (
self.x,
self.y,
self.z,
self.vx,
self.vy,
self.vz
)
def apply_gravity(curr_moons):
for m1, m2 in permutations(curr_moons, 2):
if m1.x != m2.x:
m1.vx += 1 if m1.x < m2.x else -1
if m1.y != m2.y:
m1.vy += 1 if m1.y < m2.y else -1
if m1.z != m2.z:
m1.vz += 1 if m1.z < m2.z else -1
def apply_velocity(curr_moons):
for m in curr_moons:
m.x += m.vx
m.y += m.vy
m.z += m.vz
def d12p1():
moons = [Moon(pos) for pos in d12_input]
for _ in range(1000):
apply_gravity(moons)
apply_velocity(moons)
return sum(map(lambda m: m.kinetic_energy * m.potential_energy, moons))
def lcm(a, b):
return (a*b)//math.gcd(a, b)
def d12p2():
moons = [Moon(pos) for pos in d12_input]
xpos, ypos, zpos = [set(), set(), set()]
repeated = {'x': 0, 'y': 0, 'z': 0}
count = 0
while 1:
apply_gravity(moons)
apply_velocity(moons)
xs = '|'.join([str([m.x, m.vx]) for m in moons])
ys = '|'.join([str([m.y, m.vy]) for m in moons])
zs = '|'.join([str([m.z, m.vz]) for m in moons])
if xs not in xpos:
xpos.add(xs)
elif repeated['x'] == 0:
repeated['x'] = count
if ys not in ypos:
ypos.add(ys)
elif repeated['y'] == 0:
repeated['y'] = count
if zs not in zpos:
zpos.add(zs)
elif repeated['z'] == 0:
repeated['z'] = count
if all([v > 0 for v in repeated.values()]):
break
count += 1
return lcm(lcm(repeated['x'], repeated['y']), repeated['z'])
colors = [[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0], [0.0, 0.2, 0.0, 1.0]]
def display_scene(curr_moons):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glMaterialfv(GL_FRONT, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
glTranslatef(0, 0, 0)
glutSolidSphere(.2, 60, 20)
glPopMatrix()
for index, m in enumerate(curr_moons):
glPushMatrix()
glMaterialfv(GL_FRONT, GL_DIFFUSE, colors[index])
glTranslatef(m.x/10, m.y/10, m.z/10)
glutSolidSphere(.1, 60, 20)
glPopMatrix()
apply_gravity(curr_moons)
apply_velocity(curr_moons)
glutSwapBuffers()
def timer(extra):
glutPostRedisplay()
glutTimerFunc(120, timer, 0)
def main():
moons = [Moon(pos) for pos in d12_input]
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(800, 800)
glutInitWindowPosition(350, 200)
glutCreateWindow('name')
glClearColor(0., 0., 0., 1.)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
lightZeroPosition = [10., 4., 10., 1.]
lightZeroColor = [0.8, 1.0, 0.8, 1.0]
glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
glEnable(GL_LIGHT0)
glutDisplayFunc(lambda: display_scene(moons))
glutTimerFunc(0, timer, 0)
glMatrixMode(GL_PROJECTION)
gluPerspective(40., 1., 1., 40.)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -10, 10,
0, 0, 0,
0, 1, 0)
glPushMatrix()
glutMainLoop()
if __name__ == "__main__":
print('D12P1 result:', d12p1())
print('D12P2 result:', d12p2())
main()