Update day 17 solution
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-12-17 16:58:18 +01:00
parent 9b8178438a
commit a81a510d6b

View File

@@ -3,22 +3,22 @@ from pathlib import Path
def part_1(input):
"""The max height is reached by using the maximum start y velocity that doesn't overshoot the
landing area. The x velocity doesn't mater here, because x position won't change after x
velocity reaches 0. So there will always be an vx that drops straight between the x boundaries.
Because the y velocity decreases linear we will reach our starting height after the velocity
changed exactly to the negative value of our starting velocity. At this point we have to make
sure not to overhoot the landing area, so the next velocity needs to be the same value as the
lowest coordinate of the landing area to be able to start with the highest possible starting
velocity. This leaves us with a starting velocity of abs(y_min + 1) to reach the maximum
possible height. For positive landing coordinates y_max has to be used.
"""
result = 0
_, y = input[0].rstrip().replace('target area: ', '').replace(
'x=', '').replace('y=', '').split(', ')
y_min, y_max = [int(c) for c in y.split('..')]
max_valid_height = 0
for vy_0 in range(-y_min):
vy = vy_0
y_pos = 0
max_height = 0
while y_pos > y_min:
y_pos += vy
max_height = max(max_height, y_pos)
vy -= 1
if y_min <= y_pos <= y_max:
max_valid_height = max(max_valid_height, max_height)
result = max_valid_height
y_min, _ = [int(c) for c in y.split('..')]
vy0 = abs(y_min + 1)
result = vy0 * (vy0 + 1) // 2
print("Part 1 result:", result)