From a81a510d6b4d658d01fbd67fe41a82168932334a Mon Sep 17 00:00:00 2001 From: Pascal Lais Date: Fri, 17 Dec 2021 16:58:18 +0100 Subject: [PATCH] Update day 17 solution --- day-17/day-17.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/day-17/day-17.py b/day-17/day-17.py index dcc4fb0..5974519 100644 --- a/day-17/day-17.py +++ b/day-17/day-17.py @@ -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)