Passed test case 1 & 3

This commit is contained in:
2024-12-12 17:15:50 +08:00
parent f948a2da13
commit 58f828fef3
2 changed files with 28 additions and 21 deletions

View File

@ -81,7 +81,7 @@ get_num () {
# Start the loop # Start the loop
tr " " "\n" < "$FILE" | sort -n | uniq -c > temp-input tr " " "\n" < "$FILE" | sort -n | uniq -c > temp-input
for (( i=0; i<75; i++ )) for (( i=0; i<750; i++ ))
do do
printf "%s Position %s\n" "$( date )" "$i" printf "%s Position %s\n" "$( date )" "$i"
#cat temp-input #cat temp-input

View File

@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
FILE=test-input-1 FUNCNEST=99999
FILE=test-input-2
read -r -a MAP_ARRAY <<< "$( < "$FILE" paste -s -d "" | sed -E 's/(.)/\1\ /g' )" read -r -a MAP_ARRAY <<< "$( < "$FILE" paste -s -d "" | sed -E 's/(.)/\1\ /g' )"
MAP_LEN=${#MAP_ARRAY[@]} MAP_LEN=${#MAP_ARRAY[@]}
MAP_WIDTH=$(( $( head -1 "$FILE" | wc -c ) -1 )) MAP_WIDTH=$(( $( head -1 "$FILE" | wc -c ) -1 ))
@ -45,22 +46,23 @@ check_and_unset () {
IDX=$1 IDX=$1
local KV_I=${IDX%% *} # Remove trailing whitespace local KV_I=${IDX%% *} # Remove trailing whitespace
if [[ -v KV_CACHE[$KV_I] ]] # Check if tile has been "taken" (( AREA++ ))
then VALUE=${KV_CACHE[$KV_I]}
(( AREA++ )) unset "KV_CACHE[$KV_I]" # "Take" it
VALUE=${KV_CACHE[$IDX]} printf "max: %s peri: %s area: %s key: %s val: %s\n" "$MAX_PERI" "$PERI_NUM" "$AREA" "$1" "$VALUE" >&2
printf "max: %s peri: %s area: %s key: %s val: %s\n" "$MAX_PERI" "$PERI_NUM" "$AREA" "$1" "$VALUE" >&2
(( PERI_NUM+=MAX_PERI )) (( PERI_NUM+=MAX_PERI ))
unset IFS; read -r -a VAL_ARRAY <<< "$VALUE" unset IFS; read -r -a VAL_ARRAY <<< "$VALUE"
for VAL in "${VAL_ARRAY[@]}" for VAL in "${VAL_ARRAY[@]}"
do do
(( PERI_NUM-- )) (( PERI_NUM-- ))
check_and_unset "$VAL" VAL_IDX=${VAL%% *}
done if [[ -v KV_CACHE[$VAL_IDX] ]] # Check if tile has been "taken"
then
check_and_unset "$VAL_IDX"
fi
done
unset "KV_CACHE[$KV_I]" # "Take" it
fi
} }
while read -r CHAR while read -r CHAR
@ -86,6 +88,7 @@ do
IFS=':'; while read -r VAR1 VAR2 IFS=':'; while read -r VAR1 VAR2
do do
printf "VAR1: %s VAR2: %s\n" "$VAR1" "$VAR2" >&2
KV_CACHE[$VAR1]=$VAR2 KV_CACHE[$VAR1]=$VAR2
done <<< "$( done <<< "$(
@ -94,7 +97,9 @@ do
do do
NUM_ADJ=0 NUM_ADJ=0
printf "%s:" "${CHAR_ARRAY[i]}" printf "%s:" "${CHAR_ARRAY[i]}"
for (( j=i+1; j<i+MAP_WIDTH; j++ )) # There can only be MAP_WIDTH number of elements within valid range MIN=$(( $(( i-MAP_WIDTH )) < 0 ? 0 : $(( i-MAP_WIDTH )) ))
MAX=$(( $(( i+MAP_WIDTH )) < MAP_LEN ? $(( i+MAP_WIDTH )) : $(( MAP_LEN )) ))
for (( j=MIN; j<MAX; j++ )) # There can only be MAP_WIDTH number of elements within valid range
do do
if check_adjacent "${CHAR_ARRAY[i]}" "${CHAR_ARRAY[j]}" if check_adjacent "${CHAR_ARRAY[i]}" "${CHAR_ARRAY[j]}"
then then
@ -108,13 +113,15 @@ do
)" )"
# Iterate through # Iterate through
for CHAR in "${CHAR_ARRAY[@]}" for CH in "${CHAR_ARRAY[@]}"
do do
MAX_PERI=3 CHAR=${CH%% *} # Remove trailing whitespace
PERI_NUM=1 # Special head case if ! [[ -v KV_CACHE[$CHAR] ]]; then continue; fi
MAX_PERI=4
PERI_NUM=0
AREA=0 AREA=0
check_and_unset "$CHAR" check_and_unset "$CHAR"
printf "max: %s peri: %s area: %s\n" "$MAX_PERI" "$PERI_NUM" "$AREA" >&2 printf "char: %s max: %s peri: %s area: %s\n" "$CHAR" "$MAX_PERI" "$PERI_NUM" "$AREA" >&2
printf "%s\n" "$(( PERI_NUM * AREA ))" printf "%s\n" "$(( PERI_NUM * AREA ))"
done done
# Get all plants # Get all plants