#!/usr/bin/env bash declare -A KV_CACHE read -r -a ROCK_LIST <<< "$( cat input )" DUMP_KV_CACHE=0 BLINK_NUM=75 # KV Cache provides n depth search, # but must always be fully divisible by BLINK_NUM DEPTH=5 if [[ $(( BLINK_NUM % DEPTH )) -gt 0 ]] ;then printf "BLINK_NUM DEPTH mismatch.\n" && exit ; fi ROCK_RETURN=() ROCK_NUM=0 blink_rock () { #printf "Blinking rock : %s\n" "$ROCK_NUM" >&2 # Remove leading zeroes #ROCK_NUM=$(( 10#$ROCK_NUM )) # Rule 1 : Convert 0 to 1 if [[ $ROCK_NUM -eq 0 ]] then ROCK_RETURN=( 1 ) return fi # Rule 2 : If even number of digits, split half half DIGIT_COUNT=${#ROCK_NUM} if [[ $(( DIGIT_COUNT % 2 )) -eq 0 ]] && [[ $DIGIT_COUNT -gt 0 ]] then HALF=$(( DIGIT_COUNT / 2 )) read -r -a ROCK_RETURN <<< "$( printf "%s %s " "$(( 10#${ROCK_NUM:0:$HALF} ))" "$(( 10#${ROCK_NUM:$HALF} ))" )" return fi # Rule 3 : Multiply by 2024 ROCK_RETURN=( $(( ROCK_NUM * 2024 )) ) return } ROCKS_TO_BLINK=() BLINKED_ROCKS=() # Updates the global array blink_rocks () { BLINKED_ROCKS=() for ROCK in "${ROCKS_TO_BLINK[@]}" do ROCK_NUM=$ROCK blink_rock BLINKED_ROCKS+=( "${ROCK_RETURN[@]}" ) done #printf "BLINKED_ROCKS: " >&2 #printf "%s " "${BLINKED_ROCKS[@]}" >&2 #printf "\n" >&2 } #FINAL_LIST=() # Keeps track of read -r -a FINAL_LIST <<< "$( printf "%s " "${ROCK_LIST[@]}" )" POSITION=0 while [[ $POSITION -lt $BLINK_NUM ]] do printf "%s Working on position %s\n" "$( date )" "$POSITION" INTER_LIST_1=() # Essentially move $DEPTH steps at a time for ROCK in "${FINAL_LIST[@]}" do # Check if this is already cached if [[ -v KV_CACHE["$ROCK"] ]] then # Retrieve cached entry read -r -a X <<< "${KV_CACHE[$ROCK]}" INTER_LIST_1+=( "${X[@]}" ) #printf "Cache hit. Key: %s, Value: " "$ROCK" #printf "%s " "${KV_CACHE[$ROCK]}" #printf "\n" else # Build cache entry ROCKS_TO_BLINK=( "$ROCK" ) for (( i=0; i