From 86572611c91a3925e56222aa8c0aa325bc75a0a3 Mon Sep 17 00:00:00 2001 From: Arne Wischer Date: Wed, 18 Dec 2019 13:26:30 +0100 Subject: [PATCH] finished day 12b --- days/test_day12.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/days/test_day12.py b/days/test_day12.py index 3ca6e40..08f001e 100644 --- a/days/test_day12.py +++ b/days/test_day12.py @@ -1,6 +1,9 @@ -from itertools import permutations import os import re +from copy import deepcopy +from functools import reduce +from itertools import permutations +from math import gcd inputfile = os.path.join(os.path.dirname(__file__), "input/day12_input") @@ -32,3 +35,28 @@ def test_day12a(): e += (abs(p[0])+abs(p[1])+abs(p[2]))*(abs(v[0])+abs(v[1])+abs(v[2])) assert e == 9139 + + +def test_day12b(): + """ + Finished with helpful hints from ZuBsPaCe + https://www.reddit.com/r/adventofcode/comments/e9jxh2/help_2019_day_12_part_2_what_am_i_not_seeing/far9cgu/ + """ + moons = [[m, [0, 0, 0]] for m in get_moons()] + com = [] + for i in range(3): + count = 0 + initial = deepcopy(moons) + while True: + for me, other in permutations(moons, 2): + pa = me[0] + pb = other[0] + me[1][i] += -1 if pa[i] > pb[i] else 1 if pa[i] < pb[i] else 0 + for moon in moons: + moon[0][i] += moon[1][i] + count += 1 + if moons == initial: + break + com.append(count) + steps = reduce(lambda a, b: int(a * b / gcd(a, b)), com) + assert steps == 420788524631496