From: Brett Parker Date: Tue, 8 Dec 2020 21:28:45 +0000 (+0000) Subject: Minor refactor start X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2019.git/commitdiff_plain/8559cc01c88addff1e3970519775d08c121ce5bf?ds=sidebyside Minor refactor start --- diff --git a/day10/check_asteroids.sh b/day10/check_asteroids.sh index bf54544..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 diff --git a/day10/common.sh b/day10/common.sh new file mode 100644 index 0000000..86979ed --- /dev/null +++ b/day10/common.sh @@ -0,0 +1,25 @@ +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" +} + +get_asteroids_vector() { + local a1=$1 + local a2=$2 + + local x1=${a1%,*} + local y1=${a1#*,} + local x2=${a2%,*} + local y2=${a2%*,} + + get_vector $x1 $y1 $x2 $y2 +} + +