Files
advent-of-code-2021/day-17/day-17.py
Pascal Lais 9b8178438a
All checks were successful
continuous-integration/drone/push Build is passing
Remove not needed line
2021-12-17 16:04:13 +01:00

56 lines
1.6 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
def part_1(input):
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
print("Part 1 result:", result)
def part_2(input):
result = 0
x, y = input[0].rstrip().replace('target area: ', '').replace(
'x=', '').replace('y=', '').split(', ')
x_min, x_max = [int(c) for c in x.split('..')]
y_min, y_max = [int(c) for c in y.split('..')]
for vx_0 in range(x_max):
for vy_0 in range(y_min, -y_min):
vx = vx_0
vy = vy_0
x_pos = 0
y_pos = 0
while x_pos < x_max and y_pos > y_min:
x_pos += vx
y_pos += vy
if vx != 0:
vx -= 1
vy -= 1
if x_min <= x_pos <= x_max and \
y_min <= y_pos <= y_max:
result += 1
break
print("Part 2 result:", result)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)