diff --git a/days/input/day12_input b/days/input/day12_input new file mode 100644 index 0000000..a302737 --- /dev/null +++ b/days/input/day12_input @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/days/test_day12.py b/days/test_day12.py new file mode 100644 index 0000000..3ca6e40 --- /dev/null +++ b/days/test_day12.py @@ -0,0 +1,34 @@ +from itertools import permutations +import os +import re + +inputfile = os.path.join(os.path.dirname(__file__), "input/day12_input") + + +def get_moons(): + with open(inputfile) as fp: + return [[int(m[1]), int(m[2]), int(m[3])] + for m in + re.finditer(r'x=(-?\d+).*y=(-?\d+).*z=(-?\d+)', fp.read())] + + +def test_day12a(): + moons = [(m, [0, 0, 0]) for m in get_moons()] + e = 0 + for _ in range(1000): + for me, other in permutations(moons, 2): + v = me[1] + pa = me[0] + pb = other[0] + v[0] += -1 if pa[0] > pb[0] else 1 if pa[0] < pb[0] else 0 + v[1] += -1 if pa[1] > pb[1] else 1 if pa[1] < pb[1] else 0 + v[2] += -1 if pa[2] > pb[2] else 1 if pa[2] < pb[2] else 0 + for moon in moons: + moon[0][0] += moon[1][0] + moon[0][1] += moon[1][1] + moon[0][2] += moon[1][2] + + for p, v in moons: + e += (abs(p[0])+abs(p[1])+abs(p[2]))*(abs(v[0])+abs(v[1])+abs(v[2])) + + assert e == 9139