From 4eae307beef3118206f6d890567c60e359447771 Mon Sep 17 00:00:00 2001 From: Benjo Date: Fri, 17 Dec 2021 15:31:16 +0100 Subject: [PATCH] day 17 --- 17/17.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 17/input | 1 + 17/testInput | 1 + 3 files changed, 44 insertions(+) create mode 100644 17/17.py create mode 100644 17/input create mode 100644 17/testInput diff --git a/17/17.py b/17/17.py new file mode 100644 index 0000000..dad6886 --- /dev/null +++ b/17/17.py @@ -0,0 +1,42 @@ +import math +import re + +with open("input") as file: + target = file.read().removesuffix("\n") + minX, maxX, minY, maxY = [int(v) for v in re.search(r"x=(.+)\.\.(.+), y=(.+)\.\.(.+)", target).groups()] + +# minimize the set of velocities to check via bounds +# lower x bound cant be lower than a +# where a(a+1)/2 = minX (little gauss) +# because posX loses 1 every step and posX must at least reach minX +# isolating a gives -0.5 + sqrt(0.25 + 2 * minX) +# ceiling is enough because if minX gets a bit higher then a cannot reach it anymore +xBounds = math.ceil(-0.5 + math.sqrt(0.25 + 2 * minX)), maxX + +# yPos is symmetric on the way back down and starts with 0 +yBounds = minY, -minY + +xRange = range(xBounds[0], xBounds[1] + 1) +yRange = range(yBounds[0], yBounds[1] + 1) + +validCount = 0 +allMaxYReached = [] + +# simulate all possible velocities +for vx, vy in [(x, y) for x in xRange for y in yRange]: + posX, posY = 0, 0 + maxYReached = 0 + while posX <= maxX and posY >= minY: + if posX >= minX and posY <= maxY: + # target hit + validCount += 1 + allMaxYReached.append(maxYReached) + break + posX += vx + posY += vy + vx -= 1 if vx > 0 else 0 + vy -= 1 + maxYReached = max(posY, maxYReached) + +print(max(allMaxYReached)) +print(validCount) diff --git a/17/input b/17/input new file mode 100644 index 0000000..f557e51 --- /dev/null +++ b/17/input @@ -0,0 +1 @@ +target area: x=201..230, y=-99..-65 diff --git a/17/testInput b/17/testInput new file mode 100644 index 0000000..f40609b --- /dev/null +++ b/17/testInput @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5 \ No newline at end of file