X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2019.git/blobdiff_plain/667694b94a6758e3ce751c565309ae980e76a931..8559cc01c88addff1e3970519775d08c121ce5bf:/day10/check_asteroids.sh diff --git a/day10/check_asteroids.sh b/day10/check_asteroids.sh index 8faf834..efda359 100755 --- a/day10/check_asteroids.sh +++ b/day10/check_asteroids.sh @@ -5,6 +5,8 @@ set -e basedir=$(dirname $(readlink -f ${BASH_SOURCE})) +. $basedir/common.sh + filename=${1:-$basedir/3_4_8.txt} exec 3<$filename @@ -37,18 +39,6 @@ debug_line() { echo "$line" >&2 } -get_vector() { - local x1=$1 - local y1=$2 - local x2=$3 - local y2=$4 - - x_diff=$(($x1 - $x2)) - y_diff=$(($y1 - $y2)) - - echo "$x_diff,$y_diff" -} - check_collision() { # we want to know if vector2 is going to get in the way of vector1 local vector1=$1 @@ -115,50 +105,61 @@ fast_checks() { start_x=${asteroid%,*} start_y=${asteroid#*,} - hitblocker=0 - max_x=$width max_y=$ln - # first, head left of the asteroid - for (( x=$((start_x-1)); x>=0; x-- )); do - do_blocker_check $asteroid $x $start_y + # directions: + # -1, 0 - left + # 1, 0 - right + # 0, 1 - up + # 0,-1 - down + # -1, 1 - diagonal left down + # -1,-1 - diagonal left up + # 1, 1 - diagonal right down + # 1,-1 - diagonal right up + for direction in -1,0 1,0 0,1 0,-1 -1,1 -1,-1 1,1 1,-1; do + #for direction in "-1,0" "1,0" "0,1" "0,-1"; do + stepx=${direction%,*} + stepy=${direction#*,} + do_blocker_check $start_x $start_y $stepx $stepy done - hitblocker=0 # this gets updated in do_blocker_check, reset it +} - # now head right of the asteroid - for (( x=$(($start_x+1)); x<= $width; x++ )); do - do_blocker_check $asteroid $x $start_y - done - hitblocker=0 # this gets updated in do_blocker_check, reset it +do_blocker_check() { + startx=$1 + starty=$2 + stepx=$3 + stepy=$4 - # head upwards - for (( y=$((start_y-1)); y>=0; y-- )); do - do_blocker_check $asteroid $start_x $y - done - hitblocker=0 # this gets updated in do_blocker_check, reset it + hitblocker=0 - # head down - for (( y=$((start_y+1)); y<=$ln; y++ )); do - do_blocker_check $asteroid $start_x $y - done -} + if [ $stepx -eq 0 -a $stepy -eq 0 ]; then + echo "Somehow got 0 and 0 as steps" + return + fi -do_blocker_check() { - asteroid=$1 - x=$2 - y=$3 - - if [ ${asteroids[$x,$y]+a} ]; then - if [ $hitblocker -eq 0 ]; then - cansee[$asteroid,$x,$y]=1 - cansee[$x,$y,$asteroid]=1 - hitblocker=1 - else - cantsee[$asteroid,$x,$y]=1 - cantsee[$x,$y,$asteroid]=1 + local x=$startx + local y=$starty + + # add the steps before we start the loop to not be + # on ourselves + x=$((x+$stepx)) + y=$((y+$stepy)) + + while [ $x -le $width -a $x -ge 0 -a $y -le $ln -a $y -ge 0 ]; do + if [ ${asteroids[$x,$y]+a} ]; then + if [ $hitblocker -eq 0 ]; then + cansee[$asteroid,$x,$y]=1 + cansee[$x,$y,$startx,$starty]=1 + hitblocker=1 + else + cantsee[$startx,$starty,$x,$y]=1 + cantsee[$x,$y,$startx,$starty]=1 + fi fi - fi + x=$((x+$stepx)) + y=$((y+$stepy)) + done } possibly_in_way() {