Day 12 part 2
This commit is contained in:
parent
ae041ba1d5
commit
52e1374175
|
@ -1,6 +1,8 @@
|
||||||
|
from copy import deepcopy
|
||||||
import itertools
|
import itertools
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import numpy
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -8,14 +10,33 @@ def main():
|
||||||
lines = input_file.readlines()
|
lines = input_file.readlines()
|
||||||
vector_re = re.compile(r"<x=(-?\d+), y=(-?\d+), z=(-?\d+)>")
|
vector_re = re.compile(r"<x=(-?\d+), y=(-?\d+), z=(-?\d+)>")
|
||||||
matches = [vector_re.match(line.rstrip()) for line in lines]
|
matches = [vector_re.match(line.rstrip()) for line in lines]
|
||||||
coords = [list([int(i) for i in match.groups()]) for match in matches]
|
coords = [[int(i) for i in match.groups()] for match in matches]
|
||||||
moons = [[coord, [0] * 3] for coord in coords]
|
moons_i = [[coord, [0] * 3] for coord in coords]
|
||||||
|
|
||||||
# Part 1
|
# Part 1
|
||||||
|
moons = deepcopy(moons_i)
|
||||||
for _ in range(1000):
|
for _ in range(1000):
|
||||||
do_step(moons)
|
do_step(moons)
|
||||||
print("Total moons energy:", sum(get_energy(moon) for moon in moons))
|
print("Total moons energy:", sum(get_energy(moon) for moon in moons))
|
||||||
|
|
||||||
|
# Part 2
|
||||||
|
d_cycles = [0] * 3
|
||||||
|
for d in range(3):
|
||||||
|
moons = deepcopy(moons_i)
|
||||||
|
step = 0
|
||||||
|
found_d_cycle = False
|
||||||
|
while not found_d_cycle:
|
||||||
|
do_step(moons)
|
||||||
|
step += 1
|
||||||
|
for moon, moon_i in zip(moons, moons_i):
|
||||||
|
if moon[0][d] != moon_i[0][d] or moon[1][d] != moon_i[1][d]:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
d_cycles[d] = step
|
||||||
|
found_d_cycle = True
|
||||||
|
print("Dimension cycles:", d_cycles)
|
||||||
|
print("LCM:", numpy.lcm.reduce(d_cycles))
|
||||||
|
|
||||||
def do_step(moons):
|
def do_step(moons):
|
||||||
for a, b in itertools.combinations(moons, 2):
|
for a, b in itertools.combinations(moons, 2):
|
||||||
for d in range(3):
|
for d in range(3):
|
||||||
|
|
Loading…
Reference in a new issue