From 0fe7f62d9e4aaa27f053746b4c377b54aa37e024 Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Fri, 17 Dec 2021 13:25:04 +0100 Subject: [PATCH] Day 17 --- 17/01.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 17/02.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100755 17/01.sh create mode 100755 17/02.sh diff --git a/17/01.sh b/17/01.sh new file mode 100755 index 0000000..2c1a051 --- /dev/null +++ b/17/01.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +read line +target=( $(sed 's/^.*x=\(.*\)\.\.\(.*\), y=\(.*\)\.\.\(.*\)/\1 \2 \3 \4/' <<< $line) ) + +possible_x=() +for ((i=1;i0;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 diff --git a/17/02.sh b/17/02.sh new file mode 100755 index 0000000..4d41a55 --- /dev/null +++ b/17/02.sh @@ -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[@]}