Working part 2
This commit is contained in:
@ -2,14 +2,10 @@
|
||||
|
||||
TOWEL_FILE=$1
|
||||
DESIGN_FILE=$2
|
||||
TOWEL_ITER=10
|
||||
|
||||
read -r -a TOWELS <<< "$( < "$TOWEL_FILE" tr ',' ' ' | paste -s -d " " )"
|
||||
read -r -a DESIGNS <<< "$( < "$DESIGN_FILE" paste -s -d " " )"
|
||||
|
||||
#printf "Towel %s\n" "${TOWELS[@]}"
|
||||
#printf "Design %s\n" "${DESIGNS[@]}"
|
||||
|
||||
# Check if towel is a viable match
|
||||
match_towel () {
|
||||
TOWEL=$1
|
||||
@ -27,64 +23,54 @@ match_towel () {
|
||||
}
|
||||
|
||||
TOTAL_VALID=0
|
||||
declare -A UNMATCHED_TOWEL_LIST
|
||||
# Loop through designs
|
||||
for DESIGN in "${DESIGNS[@]}"
|
||||
do
|
||||
# The idea is for each design, keep a sum of combinations
|
||||
# for each index position of the DESIGN string
|
||||
# Since the branches "multiply", just take the sum
|
||||
# at the starting index, and add it to the end
|
||||
# of the "combined" index
|
||||
# for each possible combination.
|
||||
printf "Starting design : %s\n" "$DESIGN"
|
||||
declare -A MATCH_LIST
|
||||
# Get all viable starting matches
|
||||
for TOWEL_IDX in "${!TOWELS[@]}"
|
||||
do
|
||||
TOWEL=${TOWELS[$TOWEL_IDX]}
|
||||
#printf "Testing towel: %s\n" "$TOWEL"
|
||||
declare -A POINTER_LIST
|
||||
POINTER_LIST[0]=1
|
||||
POINTER_END=$(( ${#DESIGN} ))
|
||||
|
||||
if match_towel "$TOWEL" "" "$DESIGN"
|
||||
then
|
||||
#printf "Starting match found. towel : %s\n" "$TOWEL"
|
||||
# Copy towel list and remove the starting towel
|
||||
read -r -a TOWEL_LIST <<< "$( printf "%s " "${TOWELS[@]}" )"
|
||||
unset "TOWEL_LIST[$TOWEL_IDX]"
|
||||
# Add to combinations
|
||||
MATCH_LIST["$TOWEL"]=$( printf "%s " "${TOWEL_LIST[@]}" )
|
||||
unset "TOWEL_LIST"
|
||||
fi
|
||||
done
|
||||
if [[ ${#MATCH_LIST[@]} -eq 0 ]] ; then printf "No available starting matches.\n" ; continue ; fi
|
||||
# Now iterate over starting matches
|
||||
LOOP_COUNT=${#TOWELS[@]}
|
||||
while [[ $LOOP_COUNT -gt 0 ]]
|
||||
while true
|
||||
do
|
||||
for TOWEL_MATCH in "${!MATCH_LIST[@]}"
|
||||
POINTER=${#DESIGN}
|
||||
# Pick lowest pointer value
|
||||
for P in "${!POINTER_LIST[@]}"
|
||||
do
|
||||
read -r -a TOWEL_LIST <<< "${MATCH_LIST[$TOWEL_MATCH]}"
|
||||
# Iterate over towels and find subsequent match
|
||||
for TOWEL_IDX in "${!TOWEL_LIST[@]}"
|
||||
do
|
||||
TOWEL=${TOWELS[$TOWEL_IDX]}
|
||||
if match_towel "$TOWEL" "$TOWEL_MATCH" "$DESIGN"
|
||||
then
|
||||
#printf "Match found for %s towel : %s\n" "${TOWEL_MATCH}" "$TOWEL"
|
||||
unset "TOWEL_LIST[$TOWEL_IDX]"
|
||||
# Add to combinations
|
||||
MATCH_LIST["$TOWEL_MATCH$TOWEL"]=$( printf "%s " "${TOWEL_LIST[@]}" )
|
||||
unset "MATCH_LIST[$TOWEL_MATCH]"
|
||||
fi
|
||||
done
|
||||
if [[ $P -lt $POINTER ]]
|
||||
then
|
||||
POINTER=$P
|
||||
fi
|
||||
done
|
||||
(( LOOP_COUNT-- ))
|
||||
done
|
||||
|
||||
# Calculate proper matches
|
||||
for MATCH in "${!MATCH_LIST[@]}"
|
||||
do
|
||||
if [[ $MATCH == "$DESIGN" ]]
|
||||
POINTER_VALUE=${POINTER_LIST[$POINTER]}
|
||||
# Check if end has reached
|
||||
if [[ $POINTER -eq $POINTER_END ]]
|
||||
then
|
||||
printf "Match found for design %s\n" "$DESIGN"
|
||||
(( TOTAL_VALID++ ))
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
# Remove matches
|
||||
unset MATCH_LIST
|
||||
# Loop through towels and remove earliest pointer
|
||||
for TOWEL in "${TOWELS[@]}"
|
||||
do
|
||||
TOWEL_LEN=${#TOWEL}
|
||||
END=$(( POINTER + TOWEL_LEN ))
|
||||
if [[ ${DESIGN:$POINTER:$TOWEL_LEN} == "$TOWEL" ]]
|
||||
then
|
||||
POINTER_LIST[$END]=$(( ${POINTER_LIST[$END]} + POINTER_VALUE ))
|
||||
fi
|
||||
done
|
||||
|
||||
# Done looping through for specific point
|
||||
unset "POINTER_LIST[$POINTER]"
|
||||
done
|
||||
printf "Total matches for %s is %s.\n" "$DESIGN" "${POINTER_LIST[$POINTER_END]}"
|
||||
TOTAL_VALID=$(( TOTAL_VALID + POINTER_LIST[$POINTER_END] ))
|
||||
unset "POINTER_LIST"
|
||||
done
|
||||
printf "Total valid designs: %s\n" "$TOTAL_VALID"
|
||||
|
Reference in New Issue
Block a user