diff --git a/2024/day-11/solution-2-2.sh b/2024/day-11/solution-2-2.sh index 5024f09..8edb505 100644 --- a/2024/day-11/solution-2-2.sh +++ b/2024/day-11/solution-2-2.sh @@ -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 )"