Working part 2

This commit is contained in:
2024-12-20 01:01:41 +08:00
parent fd78069221
commit 0134815002
2 changed files with 63 additions and 69 deletions

View File

@ -4,8 +4,8 @@ FILE=$1
ITER=1 ITER=1
MACHINE_ITER=999 MACHINE_ITER=999
DEBUG=false DEBUG=false
INCREMENT=1 INCREMENT=100000000000
TAIL_NUM=16 TAIL_NUM=3
# Load registry values # Load registry values
REG_A=$(grep 'Register A:' "$FILE" | cut -f2 -d: ) REG_A=$(grep 'Register A:' "$FILE" | cut -f2 -d: )
@ -13,8 +13,8 @@ REG_B=$(grep 'Register B:' "$FILE" | cut -f2 -d: )
REG_C=$(grep 'Register C:' "$FILE" | cut -f2 -d: ) REG_C=$(grep 'Register C:' "$FILE" | cut -f2 -d: )
# Rewrite registry A starting value # Rewrite registry A starting value
#REG_A=100000000000000 REG_A=100000000000000
REG_A=164541160582800 #REG_A=164541160582800
# Load input string # Load input string
read -r -a INPUT <<< "$(grep 'Program:' "$FILE" | cut -f2 -d: | tr ',' ' ' )" read -r -a INPUT <<< "$(grep 'Program:' "$FILE" | cut -f2 -d: | tr ',' ' ' )"
@ -28,8 +28,11 @@ printf "Registers A: %s B: %s C: %s\n" "$REG_A" "$REG_B" "$REG_C" >&2
. machine.sh . machine.sh
# Iterate registry A until last nth digits are matched # Iterate registry A until last nth digits are matched
while ! check_tail "$TAIL_NUM" PIN_DIGITS=0
while ! check_tail "$INPUT_LEN" "$PIN_DIGITS"
do do
while ! check_tail "$TAIL_NUM" "$PIN_DIGITS"
do
read -r -a OUTPUT <<< "$( execute_machine )" read -r -a OUTPUT <<< "$( execute_machine )"
printf "Register A: %s\n" "$REG_A" printf "Register A: %s\n" "$REG_A"
printf "Output:\t" printf "Output:\t"
@ -41,4 +44,9 @@ do
(( REG_A+=INCREMENT)) (( REG_A+=INCREMENT))
#(( ITER-- )) #(( ITER-- ))
if [[ $ITER -eq 0 ]] ; then break ; fi if [[ $ITER -eq 0 ]] ; then break ; fi
done
PIN_DIGITS=$TAIL_NUM
printf "Matched digit. Reducing count"
(( TAIL_NUM+=1 ))
(( INCREMENT/= 50 ))
done done

View File

@ -2,14 +2,10 @@
TOWEL_FILE=$1 TOWEL_FILE=$1
DESIGN_FILE=$2 DESIGN_FILE=$2
TOWEL_ITER=10
read -r -a TOWELS <<< "$( < "$TOWEL_FILE" tr ',' ' ' | paste -s -d " " )" read -r -a TOWELS <<< "$( < "$TOWEL_FILE" tr ',' ' ' | paste -s -d " " )"
read -r -a DESIGNS <<< "$( < "$DESIGN_FILE" 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 # Check if towel is a viable match
match_towel () { match_towel () {
TOWEL=$1 TOWEL=$1
@ -27,64 +23,54 @@ match_towel () {
} }
TOTAL_VALID=0 TOTAL_VALID=0
declare -A UNMATCHED_TOWEL_LIST # Loop through designs
for DESIGN in "${DESIGNS[@]}" for DESIGN in "${DESIGNS[@]}"
do 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" printf "Starting design : %s\n" "$DESIGN"
declare -A MATCH_LIST declare -A POINTER_LIST
# Get all viable starting matches POINTER_LIST[0]=1
for TOWEL_IDX in "${!TOWELS[@]}" POINTER_END=$(( ${#DESIGN} ))
do
TOWEL=${TOWELS[$TOWEL_IDX]}
#printf "Testing towel: %s\n" "$TOWEL"
if match_towel "$TOWEL" "" "$DESIGN" while true
do
POINTER=${#DESIGN}
# Pick lowest pointer value
for P in "${!POINTER_LIST[@]}"
do
if [[ $P -lt $POINTER ]]
then then
#printf "Starting match found. towel : %s\n" "$TOWEL" POINTER=$P
# 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 fi
done done
if [[ ${#MATCH_LIST[@]} -eq 0 ]] ; then printf "No available starting matches.\n" ; continue ; fi POINTER_VALUE=${POINTER_LIST[$POINTER]}
# Now iterate over starting matches # Check if end has reached
LOOP_COUNT=${#TOWELS[@]} if [[ $POINTER -eq $POINTER_END ]]
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 then
#printf "Match found for %s towel : %s\n" "${TOWEL_MATCH}" "$TOWEL" break
unset "TOWEL_LIST[$TOWEL_IDX]"
# Add to combinations
MATCH_LIST["$TOWEL_MATCH$TOWEL"]=$( printf "%s " "${TOWEL_LIST[@]}" )
unset "MATCH_LIST[$TOWEL_MATCH]"
fi fi
done
done
(( LOOP_COUNT-- ))
done
# Calculate proper matches # Loop through towels and remove earliest pointer
for MATCH in "${!MATCH_LIST[@]}" for TOWEL in "${TOWELS[@]}"
do do
if [[ $MATCH == "$DESIGN" ]] TOWEL_LEN=${#TOWEL}
END=$(( POINTER + TOWEL_LEN ))
if [[ ${DESIGN:$POINTER:$TOWEL_LEN} == "$TOWEL" ]]
then then
printf "Match found for design %s\n" "$DESIGN" POINTER_LIST[$END]=$(( ${POINTER_LIST[$END]} + POINTER_VALUE ))
(( TOTAL_VALID++ ))
fi fi
done done
# Remove matches # Done looping through for specific point
unset MATCH_LIST 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 done
printf "Total valid designs: %s\n" "$TOTAL_VALID" printf "Total valid designs: %s\n" "$TOTAL_VALID"