From f4a79d5e07e6a3526a3926ea042e193def69bb2c Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Wed, 1 Dec 2021 03:07:11 +0100 Subject: [PATCH] d12p1; p2 ... well, I hate Eric. --- day03.py | 4 +-- day04.py | 4 +-- day08.py | 4 +-- day10.py | 4 +-- day12.py | 74 +++++++++++++++++++++++++++++++++++++++++ inputs/input12 | 4 +++ inputs/test_input12_1_0 | 4 +++ inputs/test_input12_1_1 | 4 +++ inputs/test_input12_2_0 | 4 +++ inputs/test_input12_2_1 | 4 +++ 10 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 day12.py create mode 100644 inputs/input12 create mode 100644 inputs/test_input12_1_0 create mode 100644 inputs/test_input12_1_1 create mode 100644 inputs/test_input12_2_0 create mode 100644 inputs/test_input12_2_1 diff --git a/day03.py b/day03.py index ced0624..19ef84a 100644 --- a/day03.py +++ b/day03.py @@ -1,5 +1,5 @@ from aoc import AOCDay -from coordinate import Coordinate +from coordinate import Coordinate, DistanceAlgorithm from math import inf from typing import Any, Dict, List @@ -45,7 +45,7 @@ class Day(AOCDay): if x not in s[1]: continue - mDist = Coordinate(*x).getDistanceTo(Coordinate(0, 0), mode=0) + mDist = Coordinate(*x).getDistanceTo(Coordinate(0, 0), mode=DistanceAlgorithm.MANHATTAN) if mDist < minDist: minDist = mDist diff --git a/day04.py b/day04.py index 09b230e..e199649 100644 --- a/day04.py +++ b/day04.py @@ -7,7 +7,7 @@ class Day(AOCDay): test_solutions_p2 = [] def part1(self) -> Any: - low, hi = map(int, self.input[0].split("-")) + low, hi = self.getInputAsArraySplit("-", int) count = 0 for x in range(low, hi + 1): d0 = x // 100_000 @@ -24,7 +24,7 @@ class Day(AOCDay): return count def part2(self) -> Any: - low, hi = map(int, self.input[0].split("-")) + low, hi = self.getInputAsArraySplit("-", int) count = 0 for x in range(low, hi + 1): d0 = x // 100_000 diff --git a/day08.py b/day08.py index e23663a..80db074 100644 --- a/day08.py +++ b/day08.py @@ -7,7 +7,7 @@ class Day(AOCDay): test_solutions_p2 = [] def part1(self) -> Any: - img_string = self.input[0] + img_string = self.getInput() img_width = 25 img_height = 6 layers = [] @@ -35,7 +35,7 @@ class Day(AOCDay): return one_count * two_count def part2(self) -> Any: - img_string = self.input[0] + img_string = self.getInput() img_width = 25 img_height = 6 layers = [] diff --git a/day10.py b/day10.py index 6a04362..6181e80 100644 --- a/day10.py +++ b/day10.py @@ -37,7 +37,7 @@ class Day(AOCDay): def part1(self) -> Any: grid = Grid() - for y, l in enumerate(self.input): + for y, l in enumerate(self.getInput()): for x, c in enumerate(l): grid.set(Coordinate(x, y), c == '#') @@ -45,7 +45,7 @@ class Day(AOCDay): def part2(self) -> Any: grid = Grid() - for y, l in enumerate(self.input): + for y, l in enumerate(self.getInput()): for x, c in enumerate(l): grid.set(Coordinate(x, y), c == '#') diff --git a/day12.py b/day12.py new file mode 100644 index 0000000..6de0c2f --- /dev/null +++ b/day12.py @@ -0,0 +1,74 @@ +from aoc import AOCDay +from math import lcm +from re import compile, findall +from tools import compare +from typing import Any, List + +scanline_regex = compile(r"^$") + + +class Moon: + x: int + y: int + z: int + vel_x: int = 0 + vel_y: int = 0 + vel_z: int = 0 + + def __init__(self, x: int, y: int, z: int): + self.x = x + self.y = y + self.z = z + + def getEnergy(self): + potential = abs(self.x) + abs(self.y) + abs(self.z) + kinetic = abs(self.vel_x) + abs(self.vel_y) + abs(self.vel_z) + return potential * kinetic + + +def moveMoons(moons: List[Moon]): + for i, moon in enumerate(moons): + for t in range(len(moons)): + if i == t: + continue + + moon.vel_x += compare(moon.x, moons[t].x) + moon.vel_y += compare(moon.y, moons[t].y) + moon.vel_z += compare(moon.z, moons[t].z) + + for moon in moons: + moon.x += moon.vel_x + moon.y += moon.vel_y + moon.z += moon.vel_z + + +class Day(AOCDay): + test_solutions_p1 = [183, 14645] + test_solutions_p2 = [2772, 4686774924] + + def part1(self) -> Any: + moons = [Moon(*map(int, findall(scanline_regex, line)[0])) for line in self.getInput()] + + for step in range(1000): + moveMoons(moons) + + return sum([x.getEnergy() for x in moons]) + + def part2(self) -> Any: + moons = [Moon(*map(int, findall(scanline_regex, line)[0])) for line in self.getInput()] + + init_moons = moons.copy() + vel_0_pos = [0, 0, 0, 0] + moveCounter = 0 + #while 0 in vel_0_pos: + for bla in range(3000): + moveCounter += 1 + moveMoons(moons) + for i, m in enumerate(moons): + if m.x == init_moons[i].x and m.y == init_moons[i].y and m.z == init_moons[i].z and m.vel_x == 0 and m.vel_y == 0 and m.vel_z == 0: + print(i, moveCounter) + if (vel_0_pos[i] == 0 or vel_0_pos[i] > moveCounter - vel_0_pos[i]) and m.vel_x == 0 and m.vel_y == 0 and m.vel_z == 0: + vel_0_pos[i] = moveCounter - vel_0_pos[i] + + print(vel_0_pos) + return lcm(*vel_0_pos) diff --git a/inputs/input12 b/inputs/input12 new file mode 100644 index 0000000..7d3ccaf --- /dev/null +++ b/inputs/input12 @@ -0,0 +1,4 @@ + + + + diff --git a/inputs/test_input12_1_0 b/inputs/test_input12_1_0 new file mode 100644 index 0000000..89cc805 --- /dev/null +++ b/inputs/test_input12_1_0 @@ -0,0 +1,4 @@ + + + + diff --git a/inputs/test_input12_1_1 b/inputs/test_input12_1_1 new file mode 100644 index 0000000..1078293 --- /dev/null +++ b/inputs/test_input12_1_1 @@ -0,0 +1,4 @@ + + + + diff --git a/inputs/test_input12_2_0 b/inputs/test_input12_2_0 new file mode 100644 index 0000000..89cc805 --- /dev/null +++ b/inputs/test_input12_2_0 @@ -0,0 +1,4 @@ + + + + diff --git a/inputs/test_input12_2_1 b/inputs/test_input12_2_1 new file mode 100644 index 0000000..1078293 --- /dev/null +++ b/inputs/test_input12_2_1 @@ -0,0 +1,4 @@ + + + +