My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2021/17

+35
+35
2021/day17.py
··· 1 + import fileinput 2 + from utils import parse_nums 3 + 4 + 5 + def solve(vel_x=0, vel_y=0): 6 + x = 0 7 + y = 0 8 + max_y = 0 9 + 10 + while x <= END_X and y >= END_Y - 30: 11 + x += vel_x 12 + y += vel_y 13 + max_y = max(max_y, y) 14 + 15 + if vel_x > 0: 16 + vel_x -= 1 17 + elif vel_x < 0: 18 + vel_x += 1 19 + 20 + vel_y -= 1 21 + 22 + # We are in the target zone. 23 + if START_X <= x <= END_X and START_Y <= y <= END_Y: 24 + return max_y 25 + 26 + 27 + # Parse problem input. 28 + START_X, END_X, START_Y, END_Y = parse_nums(fileinput.input()[0]) 29 + 30 + # Compute viable start velocities. 31 + viables = [solve(x, y) for y in range(START_Y, -START_Y + 1) for x in range(END_X + 1)] 32 + viables = [v for v in viables if v is not None] 33 + 34 + print "Part 1:", max(viables) 35 + print "Part 2:", sum(1 for v in viables)