Files
advent-of-code-2021/17/01.sh
2021-12-17 13:25:04 +01:00

51 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
read line
target=( $(sed 's/^.*x=\(.*\)\.\.\(.*\), y=\(.*\)\.\.\(.*\)/\1 \2 \3 \4/' <<< $line) )
possible_x=()
for ((i=1;i<target[1];i++)); do
velocity_x=$i
position_x=0
while [ $velocity_x -gt 0 ] && [ $position_x -lt ${target[0]} ]; do
position_x=$((position_x+velocity_x))
velocity_x=$((velocity_x-1))
done
if [ $position_x -ge ${target[0]} ] && [ $position_x -le ${target[1]} ]; then
possible_x+=( $i )
fi
done
highest_y=0
for ((i=0;i<${#possible_x[@]};i++)); do
temp_highest_y=0
possible_y=$((target[2]*(-1)-1))
for ((j=$possible_y;j>0;j--)); do
velocity_x=${possible_x[$i]}
velocity_y=$j
position_x=0
position_y=0
while [ $position_x -le ${target[1]} ] && [ $position_y -ge ${target[2]} ]; do
position_x=$((position_x+velocity_x))
position_y=$((position_y+velocity_y))
velocity_x=$((velocity_x-1))
if [ $velocity_x -lt 0 ]; then
velocity_x=0
fi
velocity_y=$((velocity_y-1))
if [ $position_y -gt $temp_highest_y ]; then
temp_highest_y=$position_y
fi
if [ $position_x -ge ${target[0]} ] && [ $position_y -ge ${target[2]} ] \
&& [ $position_x -le ${target[1]} ] && [ $position_y -le ${target[3]} ]; then
if [ $temp_highest_y -gt $highest_y ]; then
highest_y=$temp_highest_y
fi
break
fi
done
done
done
echo $highest_y