Massive speed up with correct summation calculation

This commit is contained in:
2024-12-11 23:27:03 +08:00
parent 6e636d4c34
commit 9e60df1c2e

View File

@ -46,9 +46,9 @@ printf "Cache built. KV size: %s \n" "$( wc -l kv-cache )"
# Load cache file # Load cache file
declare -A KV_CACHE declare -A KV_CACHE
while read -r LINE while read -r KEY VALUE
do do
KV_CACHE[${LINE%% *}]="${LINE#* }" KV_CACHE[$KEY]="$VALUE"
done < kv-cache done < kv-cache
# Use this function for cache misses # Use this function for cache misses
@ -69,9 +69,8 @@ get_num () {
if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]] if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]]
then then
HALF=$(( DIGIT_COUNT / 2 )) HALF=$(( DIGIT_COUNT / 2 ))
VALUE=( "$(( 10#${NUM:0:$HALF} ))" "$(( 10#${NUM:$HALF} ))" ) printf "%s %s \n" "$OCCUR" "$(( 10#${NUM:0:$HALF} ))"
printf "%s %s \n" "$OCCUR" "${VALUE[0]}" printf "%s %s \n" "$OCCUR" "$(( 10#${NUM:$HALF} ))"
printf "%s %s \n" "$OCCUR" "${VALUE[1]}"
return return
fi fi
@ -98,11 +97,10 @@ do
get_num "$NUM" get_num "$NUM"
fi fi
done < temp-input | done < temp-input |
grep -E '[0-9]*' |
tee temp-output | tee temp-output |
sort | sort -n |
uniq -c | uniq -c |
awk '{print ($1 * $2) " " $3}' | awk '{sums[$3] += $1 * $2 } END {for (i in sums) {print sums[i], i}}' |
sponge temp-input sponge temp-input
done done
printf "Final count: %s\n" "$( awk '{print $1}' < temp-input | paste -s -d "+" | bc )" printf "Final count: %s\n" "$( awk '{print $1}' < temp-input | paste -s -d "+" | bc )"