Working part 2. Implemented sort and group for each iteration
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
FILE=input
|
FILE=input
|
||||||
ITERATIONS=75
|
|
||||||
|
|
||||||
# Get max num size by squaring the largest number
|
# Get max num size by squaring the largest number
|
||||||
MAX_NUM_SIZE=0
|
MAX_NUM_SIZE=0
|
||||||
@ -16,42 +15,32 @@ done <<< "$( < input tr " " "\n" )"
|
|||||||
|
|
||||||
# Just pre-compute a large number of digits
|
# Just pre-compute a large number of digits
|
||||||
{
|
{
|
||||||
# Rule 1
|
# Rule 1
|
||||||
printf "0 1 \n"
|
printf "0 1 \n"
|
||||||
|
|
||||||
# Rule 2 : If even number of digits, split half half
|
# Rule 2 : If even number of digits, split half half
|
||||||
for (( i=10 ; i<100; i+=1 ))
|
for (( i=10 ; i<100; i+=1 ))
|
||||||
do
|
do
|
||||||
DIGIT_COUNT=${#i}
|
DIGIT_COUNT=${#i}
|
||||||
HALF=$(( DIGIT_COUNT / 2 ))
|
HALF=$(( DIGIT_COUNT / 2 ))
|
||||||
printf "%s %s %s \n" "$i" "$(( 10#${i:0:$HALF} ))" "$(( 10#${i:$HALF} ))"
|
printf "%s %s %s \n" "$i" "$(( 10#${i:0:$HALF} ))" "$(( 10#${i:$HALF} ))"
|
||||||
done
|
done
|
||||||
for (( i=1000 ; i<10000; i+=1 ))
|
for (( i=1000 ; i<10000; i+=1 ))
|
||||||
do
|
do
|
||||||
DIGIT_COUNT=${#i}
|
DIGIT_COUNT=${#i}
|
||||||
HALF=$(( DIGIT_COUNT / 2 ))
|
HALF=$(( DIGIT_COUNT / 2 ))
|
||||||
printf "%s %s %s \n" "$i" "$(( 10#${i:0:$HALF} ))" "$(( 10#${i:$HALF} ))"
|
printf "%s %s %s \n" "$i" "$(( 10#${i:0:$HALF} ))" "$(( 10#${i:$HALF} ))"
|
||||||
done
|
done
|
||||||
for (( i=100000 ; i<1000000; i+=1 ))
|
|
||||||
do
|
|
||||||
DIGIT_COUNT=${#i}
|
|
||||||
HALF=$(( DIGIT_COUNT / 2 ))
|
|
||||||
printf "%s %s %s \n" "$i" "$(( 10#${i:0:$HALF} ))" "$(( 10#${i:$HALF} ))"
|
|
||||||
done
|
|
||||||
|
|
||||||
# Rule 3
|
# Rule 3
|
||||||
for (( i=1; i<10; i+=1 ))
|
for (( i=1; i<10; i+=1 ))
|
||||||
do
|
do
|
||||||
printf "%s %s \n" "$i" "$(( i * 2024 ))"
|
printf "%s %s \n" "$i" "$(( i * 2024 ))"
|
||||||
done
|
done
|
||||||
for (( i=100; i<1000; i+=1 ))
|
for (( i=100; i<1000; i+=1 ))
|
||||||
do
|
do
|
||||||
printf "%s %s \n" "$i" "$(( i * 2024 ))"
|
printf "%s %s \n" "$i" "$(( i * 2024 ))"
|
||||||
done
|
done
|
||||||
for (( i=10000; i<100000; i+=1 ))
|
|
||||||
do
|
|
||||||
printf "%s %s \n" "$i" "$(( i * 2024 ))"
|
|
||||||
done
|
|
||||||
} | sort -n > kv-cache
|
} | sort -n > kv-cache
|
||||||
printf "Cache built. KV size: %s \n" "$( wc -l kv-cache )"
|
printf "Cache built. KV size: %s \n" "$( wc -l kv-cache )"
|
||||||
|
|
||||||
@ -62,6 +51,7 @@ do
|
|||||||
KV_CACHE[${LINE%% *}]="${LINE#* }"
|
KV_CACHE[${LINE%% *}]="${LINE#* }"
|
||||||
done < kv-cache
|
done < kv-cache
|
||||||
|
|
||||||
|
# Use this function for cache misses
|
||||||
NUM=0
|
NUM=0
|
||||||
get_num () {
|
get_num () {
|
||||||
|
|
||||||
@ -70,7 +60,7 @@ get_num () {
|
|||||||
# Rule 1 : Convert 0 to 1
|
# Rule 1 : Convert 0 to 1
|
||||||
if [[ $NUM -eq 0 ]]
|
if [[ $NUM -eq 0 ]]
|
||||||
then
|
then
|
||||||
printf "1 \n"
|
printf "%s 1 \n" "$OCCUR"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -79,31 +69,40 @@ get_num () {
|
|||||||
if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]]
|
if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]]
|
||||||
then
|
then
|
||||||
HALF=$(( DIGIT_COUNT / 2 ))
|
HALF=$(( DIGIT_COUNT / 2 ))
|
||||||
printf "%s %s " "$(( 10#${NUM:0:$HALF} ))" "$(( 10#${NUM:$HALF} ))"
|
VALUE=( "$(( 10#${NUM:0:$HALF} ))" "$(( 10#${NUM:$HALF} ))" )
|
||||||
|
printf "%s %s \n" "$OCCUR" "${VALUE[0]}"
|
||||||
|
printf "%s %s \n" "$OCCUR" "${VALUE[1]}"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Rule 3 : Multiply by 2024
|
# Rule 3 : Multiply by 2024
|
||||||
printf "%s \n" "$(( NUM * 2024 ))"
|
printf "%s %s \n" "$OCCUR" "$(( NUM * 2024 ))"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# Start the loop
|
# Start the loop
|
||||||
tr " " "\n" < "$FILE" > temp-input
|
tr " " "\n" < "$FILE" | sort -n | uniq -c > temp-input
|
||||||
for (( i=0; i<75; i++ ))
|
for (( i=0; i<75; i++ ))
|
||||||
do
|
do
|
||||||
printf "%s Position %s\n" "$( date )" "$i"
|
printf "%s Position %s\n" "$( date )" "$i"
|
||||||
while read -r NUM
|
#cat temp-input
|
||||||
|
while read -r OCCUR NUM
|
||||||
do
|
do
|
||||||
if [[ -v ${KV_CACHE[$NUM]} ]]
|
if [[ -v KV_CACHE[$NUM] ]]
|
||||||
then
|
then
|
||||||
printf "%s \n" "${KV_CACHE[$NUM]}"
|
for VAL in ${KV_CACHE[$NUM]}
|
||||||
|
do
|
||||||
|
printf "%s %s \n" "$OCCUR" "$VAL"
|
||||||
|
done
|
||||||
else
|
else
|
||||||
get_num "$NUM"
|
get_num "$NUM"
|
||||||
fi
|
fi
|
||||||
done < temp-input |
|
done < temp-input |
|
||||||
tr " " "\n" |
|
grep -E '[0-9]*' |
|
||||||
grep -Eo '[0-9]*' |
|
tee temp-output |
|
||||||
|
sort |
|
||||||
|
uniq -c |
|
||||||
|
awk '{print ($1 * $2) " " $3}' |
|
||||||
sponge temp-input
|
sponge temp-input
|
||||||
done
|
done
|
||||||
printf "Final count: %s\n" "$(wc -l < temp-input)"
|
printf "Final count: %s\n" "$( awk '{print $1}' < temp-input | paste -s -d "+" | bc )"
|
||||||
|
Reference in New Issue
Block a user