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