From: Brett Parker Date: Fri, 17 Dec 2021 11:17:42 +0000 (+0000) Subject: Make computer much faster by not using subshells X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2019.git/commitdiff_plain/09b6a3fd0fae26e58acfcfb024ab535829651a9d?hp=e40766a241079d2d16f8b58f8b65b968fd95249e Make computer much faster by not using subshells --- diff --git a/day13/computer.sh b/day13/computer.sh index 1e9c60e..098d985 100644 --- a/day13/computer.sh +++ b/day13/computer.sh @@ -15,7 +15,7 @@ get_value() { da[$index]=0 fi - echo "${da[$index]}" + declare -g __comp_value="${da[$index]}" } get_param() { @@ -24,7 +24,8 @@ get_param() { 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 } @@ -38,13 +39,15 @@ get_param_loc() { case $mode in 0) get_value dapl $pos + declare -g __comp_param_loc=$__comp_value ;; 1) - echo $pos + declare -g __comp_param_loc=$pos ;; 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" @@ -75,9 +78,12 @@ run_program() { 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) @@ -90,21 +96,25 @@ run_program() { 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 - 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 - 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 @@ -114,12 +124,15 @@ run_program() { ;; 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 - 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 @@ -134,7 +147,8 @@ run_program() { 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)