From 8559cc01c88addff1e3970519775d08c121ce5bf Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Tue, 8 Dec 2020 21:28:45 +0000 Subject: [PATCH] Minor refactor start --- day10/check_asteroids.sh | 14 ++------------ day10/common.sh | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 day10/common.sh 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 +} + + -- 2.39.5