This commit is contained in:
75
day-17/day-17.py
Normal file
75
day-17/day-17.py
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def part_1(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('..')]
|
||||
vx_0 = 0
|
||||
min_num_steps = 0
|
||||
while True:
|
||||
vx_0 += 1
|
||||
x_end = vx_0 * (vx_0 + 1) / 2
|
||||
if x_min <= x_end <= x_max:
|
||||
min_num_steps = vx_0
|
||||
break
|
||||
elif x_end > x_max:
|
||||
break
|
||||
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('..')]
|
||||
vx_0 = 0
|
||||
num_steps = 0
|
||||
while True:
|
||||
vx_0 += 1
|
||||
x_end = vx_0 * (vx_0 + 1) / 2
|
||||
if x_min <= x_end <= x_max:
|
||||
num_steps = vx_0
|
||||
elif x_end > x_max:
|
||||
break
|
||||
for vx_0 in range(x_max + 1):
|
||||
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)
|
||||
Reference in New Issue
Block a user