]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/commitdiff
Use a variable reference for where to store the result, stop putting all results...
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Tue, 15 Dec 2020 16:30:20 +0000 (16:30 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Tue, 15 Dec 2020 16:30:20 +0000 (16:30 +0000)
day15/memory.sh

index 4f16b3ba6358d4ba309130f58adc53c138713c5b..a2d954747d57e159c201837ef2cd09d7fd9b063e 100755 (executable)
@@ -12,10 +12,11 @@ read_file() {
 get_answer() {
     local number=$1
     local count=$2
 get_answer() {
     local number=$1
     local count=$2
+    local -n __fn_result=$3
     if [ ${lastseen[$number]+a} ]; then
     if [ ${lastseen[$number]+a} ]; then
-        answers[$count]=$((count-${lastseen[$number]}-1))
+        __fn_result=$((count-${lastseen[$number]}))
     else
     else
-        answers[$count]=0
+        __fn_result=0
     fi
 }
 
     fi
 }
 
@@ -39,40 +40,40 @@ filename="${1:-p1_436.txt}"
 read_file "$filename"
 
 declare -A lastseen
 read_file "$filename"
 
 declare -A lastseen
-declare -a answers
 
 current_answer=-1
 last_answer=-1
 count=0
 
 
 current_answer=-1
 last_answer=-1
 count=0
 
+result=
+
 for number in ${data[@]}; do
     count=$((count+1))
     last_answer=$current_answer
     if [ $last_answer -ge 0 ]; then
         lastseen[$last_answer]=$((count-1))
     fi
 for number in ${data[@]}; do
     count=$((count+1))
     last_answer=$current_answer
     if [ $last_answer -ge 0 ]; then
         lastseen[$last_answer]=$((count-1))
     fi
-    answers[$count]=$number
     current_answer=$number
 done
 
 while [ $count -lt 2020 ]; do
     current_answer=$number
 done
 
 while [ $count -lt 2020 ]; do
-    count=$((count+1))
-    get_answer $current_answer $count
     last_answer=$current_answer
     last_answer=$current_answer
+    get_answer $current_answer $count result
+    count=$((count+1))
+    current_answer=$result
     lastseen[$last_answer]=$((count-1))
     lastseen[$last_answer]=$((count-1))
-    current_answer=${answers[$count]}
-    $debug_func "$count: ${answers[$count]}"
+    $debug_func "$count: $current_answer"
 done
 
 done
 
-echo "Part 1: ${answers[$count]}"
+echo "Part 1: $current_answer"
 
 while [ $count -lt 30000000 ]; do
 
 while [ $count -lt 30000000 ]; do
-    count=$((count+1))
-    get_answer $current_answer $count
     last_answer=$current_answer
     last_answer=$current_answer
+    get_answer $current_answer $count result
+    count=$((count+1))
+    current_answer=$result
     lastseen[$last_answer]=$((count-1))
     lastseen[$last_answer]=$((count-1))
-    current_answer=${answers[$count]}
-    $debug_func "$count: ${answers[$count]}"
+    $debug_func "$count: $current_answer"
 done
 
 done
 
-echo "Part 2: ${answers[$count]}"
+echo "Part 2: $current_answer"