Files
advent_of_code/2021/17.py
2021-12-19 01:16:01 -05:00

32 lines
1016 B
Python

import sys, re
with open('./2021/17.input') as input_file:
tgt_x_min, tgt_x_max, tgt_y_min, tgt_y_max = map(
int, re.match("^target area: x=(.+?)\.\.(.+?), y=(.+?)\.\.(.+?)$",
input_file.read().strip()).groups())
highest = 0
unique = 0
x_range = max(abs(tgt_x_min), abs(tgt_x_max))
y_range = max(abs(tgt_y_min), abs(tgt_y_max))
for vel_y_0 in range(-y_range, y_range + 1):
for vel_x_0 in range(-x_range, x_range + 1):
pos_x, pos_y = 0, 0
vel_x, vel_y = vel_x_0, vel_y_0
height = 0
while pos_x <= tgt_x_max and pos_y >= tgt_y_min:
pos_x += vel_x
pos_y += vel_y
vel_x -= (vel_x > 0) - (vel_x < 0)
vel_y -= 1
height = max(height, pos_y)
if (tgt_x_min <= pos_x <= tgt_x_max and
tgt_y_min <= pos_y <= tgt_y_max):
highest = max(highest, height)
unique += 1
break
print("Part 1:", highest)
print("Part 2:", unique)