-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
68 lines (56 loc) · 2.01 KB
/
12.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
from math import gcd
import re
import os
inputFile = open(os.path.dirname(__file__) + '/input.txt', 'r')
lines = [line.rstrip('\n') for line in inputFile]
inputFile.close()
def solve():
positions = []
velocities = []
periods = [0, 0, 0]
for line in lines:
x, y, z = [int(num) for num in re.findall(r'[\d-]+', line)]
positions.append([x, y, z])
velocities.append([0, 0, 0])
states = [{}, {}, {}]
step = lcm = 0
while not lcm or step < 1000:
# For part 2 find the period of each axis then find their LCM
if not lcm:
for p in range(3):
if periods[p] == 0:
state = ''
for i in range(4):
state += str(positions[i][p]) + ',' + str(velocities[i][p]) + ' '
if state in states[p]:
periods[p] = step
else:
states[p][state] = 1
if 0 not in periods:
lcm = 1
for period in periods:
lcm = lcm * period // gcd(lcm, period)
for i in range(4):
for j in range(i + 1, 4):
for p in range(3):
if positions[i][p] < positions[j][p]:
velocities[i][p] += 1
velocities[j][p] -= 1
elif positions[i][p] > positions[j][p]:
velocities[j][p] += 1
velocities[i][p] -= 1
for i in range(4):
for p in range(3):
positions[i][p] += velocities[i][p]
step += 1
if step == 1000:
part_one = 0
for i in range(4):
potential = kinetic = 0
for p in range(3):
potential += abs(positions[i][p])
kinetic += abs(velocities[i][p])
part_one += potential * kinetic
print(f'Part one: {part_one}')
print(f'Part two: {lcm}')
solve()