]> git.sommitrealweird.co.uk Git - advent-of-code-2019.git/commitdiff
Make computer much faster by not using subshells
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Fri, 17 Dec 2021 11:17:42 +0000 (11:17 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Fri, 17 Dec 2021 11:17:42 +0000 (11:17 +0000)
day13/computer.sh

index 1e9c60e85985af7b2190ba43dda88943d5f1d3a8..098d985fe88ffe634dc3d200aa0cb39f1442fa77 100644 (file)
@@ -15,7 +15,7 @@ get_value() {
         da[$index]=0
     fi
 
         da[$index]=0
     fi
 
-    echo "${da[$index]}"
+    declare -g __comp_value="${da[$index]}"
 }
 
 get_param() {
 }
 
 get_param() {
@@ -24,7 +24,8 @@ get_param() {
     local mode=$3
     local relative_base=$4
 
     local mode=$3
     local relative_base=$4
 
-    loc=$(get_param_loc dap $pos $mode $relative_base)
+    get_param_loc dap $pos $mode $relative_base
+    loc=$__comp_param_loc
 
     get_value dap $loc
 }
 
     get_value dap $loc
 }
@@ -38,13 +39,15 @@ get_param_loc() {
     case $mode in
         0)
             get_value dapl $pos
     case $mode in
         0)
             get_value dapl $pos
+            declare -g __comp_param_loc=$__comp_value
             ;;
         1)
             ;;
         1)
-            echo $pos
+            declare -g __comp_param_loc=$pos
             ;;
         2)
             ;;
         2)
-            move_by=$(get_value dapl $pos)
-            echo $((relative_base+$move_by))
+            get_value dapl $pos
+            move_by=$__comp_value
+            declare -g __comp_param_loc=$((relative_base+$move_by))
             ;;
         *)
             echo "Unknown parameter mode: $mode"
             ;;
         *)
             echo "Unknown parameter mode: $mode"
@@ -75,9 +78,12 @@ run_program() {
                 if [ $instruction == "02" ]; then
                     symbol="*"
                 fi
                 if [ $instruction == "02" ]; then
                     symbol="*"
                 fi
-                val1=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
-                val2=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
-                res_loc=$(get_param_loc data $((pos+3)) ${immediate[3]} $relative_base)
+                get_param data $((pos+1)) ${immediate[1]} $relative_base
+                val1=$__comp_value
+                get_param data $((pos+2)) ${immediate[2]} $relative_base
+                val2=$__comp_value
+                get_param_loc data $((pos+3)) ${immediate[3]} $relative_base
+                res_loc=$__comp_param_loc
                 data[$res_loc]=$(($val1 $symbol $val2))
                 ;;
             03)
                 data[$res_loc]=$(($val1 $symbol $val2))
                 ;;
             03)
@@ -90,21 +96,25 @@ run_program() {
                 echo >&2
                 echo "$instruction: '$input'" >&2
                 echo >&2
                 echo >&2
                 echo "$instruction: '$input'" >&2
                 echo >&2
-                res_loc=$(get_param_loc data $((pos+1)) ${immediate[1]} $relative_base)
+                get_param_loc data $((pos+1)) ${immediate[1]} $relative_base
+                res_loc=$__comp_param_loc
                 data[$res_loc]=$input
                 param_count=1
                 ;;
             04)
                 # 04 - output
                 data[$res_loc]=$input
                 param_count=1
                 ;;
             04)
                 # 04 - output
-                val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
+                get_param data $((pos+1)) ${immediate[1]} $relative_base
+                val=$__comp_value
                 echo -n $'\r'"    "$'\r' >&2
                 echo $val
                 param_count=1
                 ;;
             05|06)
                 # 05 - jump-if-true, 06 - jump-if-false
                 echo -n $'\r'"    "$'\r' >&2
                 echo $val
                 param_count=1
                 ;;
             05|06)
                 # 05 - jump-if-true, 06 - jump-if-false
-                val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
-                jumpto=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
+                get_param data $((pos+1)) ${immediate[1]} $relative_base
+                val=$__comp_value
+                get_param data $((pos+2)) ${immediate[2]} $relative_base
+                jumpto=$__comp_value
                 param_count=2
                 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
                     ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
                 param_count=2
                 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
                     ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
@@ -114,12 +124,15 @@ run_program() {
                 ;;
             07|08)
                 # 07 - is less than, 08 - is equal
                 ;;
             07|08)
                 # 07 - is less than, 08 - is equal
-                val1=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
-                val2=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
+                get_param data $((pos+1)) ${immediate[1]} $relative_base
+                val1=$__comp_value
+                get_param data $((pos+2)) ${immediate[2]} $relative_base
+                val2=$__comp_value >&90
                 echo >&2
                 echo "$instruction: '$val1' '$val2'" >&2
                 echo >&2
                 echo >&2
                 echo "$instruction: '$val1' '$val2'" >&2
                 echo >&2
-                res_pos=$(get_param_loc data $((pos+3)) ${immediate[3]} $relative_base)
+                get_param_loc data $((pos+3)) ${immediate[3]} $relative_base
+                res_pos=$__comp_param_loc
                 data[$res_pos]=0
                 if [ "$instruction" == "07" ]; then
                     if [ $val1 -lt $val2 ]; then
                 data[$res_pos]=0
                 if [ "$instruction" == "07" ]; then
                     if [ $val1 -lt $val2 ]; then
@@ -134,7 +147,8 @@ run_program() {
             09)
                 # adjust relative base
                 param_count=1
             09)
                 # adjust relative base
                 param_count=1
-                val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
+                get_param data $((pos+1)) ${immediate[1]} $relative_base
+                val=$__comp_value
                 relative_base=$((relative_base+$val))
                 ;;
             99)
                 relative_base=$((relative_base+$val))
                 ;;
             99)