From 52e137417541e49ce80fa75945ca7ab71e8237c8 Mon Sep 17 00:00:00 2001 From: Dece Date: Fri, 13 Dec 2019 09:00:10 +0100 Subject: [PATCH] Day 12 part 2 --- 2019/day12.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/2019/day12.py b/2019/day12.py index 706bb69..18407e2 100644 --- a/2019/day12.py +++ b/2019/day12.py @@ -1,6 +1,8 @@ +from copy import deepcopy import itertools import re +import numpy def main(): @@ -8,14 +10,33 @@ def main(): lines = input_file.readlines() vector_re = re.compile(r"") matches = [vector_re.match(line.rstrip()) for line in lines] - coords = [list([int(i) for i in match.groups()]) for match in matches] - moons = [[coord, [0] * 3] for coord in coords] + coords = [[int(i) for i in match.groups()] for match in matches] + moons_i = [[coord, [0] * 3] for coord in coords] # Part 1 + moons = deepcopy(moons_i) for _ in range(1000): do_step(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): for a, b in itertools.combinations(moons, 2): for d in range(3):