diff --git a/day17.py b/day17.py new file mode 100644 index 0000000..f7121a3 --- /dev/null +++ b/day17.py @@ -0,0 +1,44 @@ +from tools.aoc import AOCDay +from typing import Any + + +def fire(velX: int, velY: int, minX: int, maxX: int, minY: int, maxY: int) -> (bool, int): + highY, posX, posY = 0, velX, velY + while not posX > maxX and not posY < minY: + if velX > 0: + velX -= 1 + velY -= 1 + + posX += velX + posY += velY + if posY > highY: + highY = posY + + hit = minX <= posX - velX <= maxX and minY <= posY - velY <= maxY + return hit, highY if hit else minY + + +class Day(AOCDay): + test_solutions_p1 = [45, 5151] + test_solutions_p2 = [112, 968] + + def getBoundaries(self) -> (int, int, int, int): + _, ranges = self.getInput().split("=", 1) + xRange, yRange = ranges.split(", y=") + minX, maxX = map(int, xRange.split("..")) + minY, maxY = map(int, yRange.split("..")) + return minX, maxX, minY, maxY + + def part1(self) -> Any: + minX, maxX, minY, maxY = self.getBoundaries() + return max(fire(x, y, minX, maxX, minY, maxY)[1] for x in range(maxX) for y in range(abs(minY))) + + def part2(self) -> Any: + minX, maxX, minY, maxY = self.getBoundaries() + hitCounter = 0 + for x in range(maxX + 1): + for y in range(minY, abs(minY)): + h, _ = fire(x, y, minX, maxX, minY, maxY) + hitCounter += h + + return hitCounter diff --git a/inputs/input17 b/inputs/input17 new file mode 100644 index 0000000..f1b6f82 --- /dev/null +++ b/inputs/input17 @@ -0,0 +1 @@ +target area: x=135..155, y=-102..-78 diff --git a/inputs/test_input17_1_0 b/inputs/test_input17_1_0 new file mode 100644 index 0000000..a07e02d --- /dev/null +++ b/inputs/test_input17_1_0 @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5 diff --git a/inputs/test_input17_1_1 b/inputs/test_input17_1_1 new file mode 100644 index 0000000..f1b6f82 --- /dev/null +++ b/inputs/test_input17_1_1 @@ -0,0 +1 @@ +target area: x=135..155, y=-102..-78 diff --git a/inputs/test_input17_2_0 b/inputs/test_input17_2_0 new file mode 100644 index 0000000..a07e02d --- /dev/null +++ b/inputs/test_input17_2_0 @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5 diff --git a/inputs/test_input17_2_1 b/inputs/test_input17_2_1 new file mode 100644 index 0000000..f1b6f82 --- /dev/null +++ b/inputs/test_input17_2_1 @@ -0,0 +1 @@ +target area: x=135..155, y=-102..-78