#!/usr/bin/env bash 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 MATCH=$2 FULL_DESIGN=$3 TO_MATCH="$MATCH$TOWEL" PARTIAL_DESIGN=${FULL_DESIGN::${#TO_MATCH}} if [[ $PARTIAL_DESIGN == "$TO_MATCH" ]] then return 0 else return 1 fi } TOTAL_VALID=0 declare -A UNMATCHED_TOWEL_LIST for DESIGN in "${DESIGNS[@]}" do 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" 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 ]] do for TOWEL_MATCH in "${!MATCH_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 done (( LOOP_COUNT-- )) done # Calculate proper matches for MATCH in "${!MATCH_LIST[@]}" do if [[ $MATCH == "$DESIGN" ]] then printf "Match found for design %s\n" "$DESIGN" (( TOTAL_VALID++ )) fi done # Remove matches unset MATCH_LIST done printf "Total valid designs: %s\n" "$TOTAL_VALID"