d12p1; p2 ... well, I hate Eric.

This commit is contained in:
Stefan Harmuth 2021-12-01 03:07:11 +01:00
parent 426b3ef609
commit f4a79d5e07
10 changed files with 102 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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 = []

View File

@ -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 == '#')

74
day12.py Normal file
View File

@ -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"^<x=(.*), y=(.*), z=(.*)>$")
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)

4
inputs/input12 Normal file
View File

@ -0,0 +1,4 @@
<x=-9, y=-1, z=-1>
<x=2, y=9, z=5>
<x=10, y=18, z=-12>
<x=-6, y=15, z=-7>

4
inputs/test_input12_1_0 Normal file
View File

@ -0,0 +1,4 @@
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>

4
inputs/test_input12_1_1 Normal file
View File

@ -0,0 +1,4 @@
<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>

4
inputs/test_input12_2_0 Normal file
View File

@ -0,0 +1,4 @@
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>

4
inputs/test_input12_2_1 Normal file
View File

@ -0,0 +1,4 @@
<x=-8, y=-10, z=0>
<x=5, y=5, z=10>
<x=2, y=-7, z=3>
<x=9, y=-8, z=-3>