This commit is contained in:
2021-12-17 13:25:04 +01:00
parent 0a9585b439
commit 0fe7f62d9e
2 changed files with 95 additions and 0 deletions

50
17/01.sh Executable file
View File

@@ -0,0 +1,50 @@
#!/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

45
17/02.sh Executable file
View File

@@ -0,0 +1,45 @@
#!/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
declare -A possible_velocities
for ((i=0;i<${#possible_x[@]};i++)); do
possible_y=$((target[2]*(-1)-1))
for ((j=$possible_y;j>=${target[2]};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_x -ge ${target[0]} ] && [ $position_y -ge ${target[2]} ] \
&& [ $position_x -le ${target[1]} ] && [ $position_y -le ${target[3]} ]; then
possible_velocities[${possible_x[$i]},$j]=1
break
fi
done
done
done
echo ${#possible_velocities[@]}