day 20 part 2 (bash): final version

This commit is contained in:
2021-07-28 16:38:42 +02:00
parent 44c9d00b85
commit fbb553645e
4 changed files with 34 additions and 412 deletions

View File

@@ -8,9 +8,8 @@ shopt -s extglob
source tile.bash
#declare -A strings
declare -a strings T R B L RT RR RB RL nums
declare -a final finalfoo
declare -a final
declare -A FINAL
declare -a CORNER EDGE CENTRAL
declare -i count=-1 SQUARE
@@ -41,7 +40,6 @@ case "$count" in
exit 0
;;
esac
printf "size=%d\n" "$SQUARE"
declare top bottom left right
declare fliptop flipbottom flipleft flipright
@@ -68,9 +66,7 @@ for key in "${!nums[@]}"; do
RL+=("$flipleft")
done
#echo BORDERS:
#print_all_borders
#exit 0
ALL="${T[*]} ${R[*]} ${B[*]} ${L[*]} ${RT[*]} ${RR[*]} ${RB[*]} ${RL[*]} "
ALLSIZE=${#ALL}
@@ -83,7 +79,6 @@ for ((i=0; i<${#nums[@]}; ++i)); do
((c += (ALLSIZE-${#S})/10))
done
# 6 is 4 for itself, + 2 for other matching
#printf "%s: %d\n" "${nums[$i]}" "$c"
case "$c" in
6)
CORNER+=("$i")
@@ -98,60 +93,29 @@ for ((i=0; i<${#nums[@]}; ++i)); do
done
printf "CENTRAL[%d]=%s\n" "${#CENTRAL[@]}" "${CENTRAL[*]}"
printf "EDGE[%d]=%s\n" "${#EDGE[@]}" "${EDGE[*]}"
printf "CORNER[%d]=%s\n" "${#CORNER[@]}" "${CORNER[*]}"
for ((i=0; i<${#CORNER[@]}; ++i)); do
printf "corner[%d]=%d\n" "$i" "${nums[$i]}"
done
#printf "ALL=%s\n" "$ALL"
printf "ALLSIZE=%d\n" "$ALLSIZE"
printf "%s : count=%d\n" "$CMD" "$count"
for ((row=0; row<SQUARE; ++row)); do
for ((col=0; col<SQUARE; ++col)); do
for k in "${!FINAL[@]}"; do
printf "final[%s]=%d\n" "$k" "${FINAL[$k]}"
done
found=0
printf "square(%d,%d)\n" "$row" "$col"
################################## 1st tile (top left)
if ((row==0 && col==0)); then
# Choose one corner for top-left, find the correct orientation
i=${CORNER[0]}
printf "corner[0]=%d\n" "${nums[$i]}"
unset "CORNER[0]"
CORNER=("${CORNER[@]}")
print_tile "$i"; echo
t=${L[$i]}
S=${ALL//$t}
printf "left matches=%d\n" $((ALLSIZE-${#S}))
t=${T[$i]}
S=${ALL//$t}
printf "top matches=%d\n" $((ALLSIZE-${#S}))
t=${B[$i]}
S=${ALL//$t}
# flip vertical if bottom is a corner side
printf "bottom matches=%d\n" $((ALLSIZE-${#S}))
if (( ALLSIZE-${#S} == 10 )); then
echo FLIP_V
flip_v "$i"
fi
print_tile "$i"; echo
t=${R[$i]}
S=${ALL//$t}
# flip horizontal if right is a corner side
printf "right matches=%d\n" $((ALLSIZE-${#S}))
if (( ALLSIZE-${#S} == 10 )); then
echo FLIP_H
flip_h "$i"
fi
print_tile "$i"
FINAL[0,0]="$i"
final+=("${nums[$i]}")
final_add "$row" "$i"
continue
fi
@@ -163,7 +127,6 @@ for ((row=0; row<SQUARE; ++row)); do
list=("${CORNER[@]}")
fi
l=${FINAL[0,$((col-1))]}
#printf "FINAL[0,%d]=%d\n" "$((col-1))" "${FINAL[0,$((col-1))]}"
right right "${strings[$l]}"
index=0
for j in "${list[@]}"; do
@@ -173,23 +136,15 @@ for ((row=0; row<SQUARE; ++row)); do
# 10 is line size
((c = (LENGTH-${#S})/10))
if ((c > 0)); then
echo "border tile match: idx=$index $j [${nums[$j]}]"
print_tile "$j"
FINAL[$row,$col]="$j"
final+=("${nums[$j]}")
echo "c=$c"
attach_left "$right" "$j"
final_add "$row" "$j"
if ((col < SQUARE-1)); then
unset "EDGE[$index]"
EDGE=("${EDGE[@]}")
printf "after removing EDGE $j\n"
printf "EDGE[%d]=%s\n" "${#EDGE[@]}" "${EDGE[*]}"
else
unset "CORNER[$index]"
CORNER=("${CORNER[@]}")
printf "after removing CORNER $j\n"
printf "CORNER[%d]=%s\n" "${#CORNER[@]}" "${CORNER[*]}"
fi
found=1
break
@@ -197,7 +152,6 @@ for ((row=0; row<SQUARE; ++row)); do
((index++))
done
if ((found==0)); then
printf "NOT FOUND\n"
exit 0
fi
continue
@@ -210,25 +164,16 @@ for ((row=0; row<SQUARE; ++row)); do
list=("${CORNER[@]}")
fi
l=${FINAL[$((row-1)),$col]}
printf "FINAL[%d,%d]=%d\n" "$((row-1))" "$col" "$l"
printf "UPPER TILE"
print_tile "$j"
bottom bottom "${strings[$l]}"
printf "UPPER bottom line: %s\n" "$bottom"
index=0
for j in "${list[@]}"; do
printf "looking for tile %d [%d]\n" "$j" "${nums[$j]}"
SIDES="${T[$j]} ${R[$j]} ${B[$j]} ${L[$j]} ${RT[$j]} ${RR[$j]} ${RB[$j]} ${RL[$j]}"
LENGTH=${#SIDES}
S=${SIDES//$bottom}
# 10 is line size
((c = (LENGTH-${#S})/10))
if ((c > 0)); then
echo "border tile match: ${nums[$j]}"
print_tile "$j"
FINAL[$row,$col]="$j"
final+=("${nums[$j]}")
echo "c=$c"
attach_top "$bottom" "$j"
final_add "$row" "$j"
if ((row < SQUARE-1)); then
@@ -244,7 +189,6 @@ for ((row=0; row<SQUARE; ++row)); do
((index++))
done
if ((found==0)); then
printf "NOT FOUND\n"
exit 0
fi
continue
@@ -252,7 +196,6 @@ for ((row=0; row<SQUARE; ++row)); do
################################## rest of last row
if ((row == SQUARE-1)); then
l=${FINAL[$((row-1)),$col]}
printf "FINAL[%d,%d]=%d\n" "$((row-1))" "$col" "${FINAL[$((row-1)),$col]}"
bottom bottom "${strings[$l]}"
index=0
for j in "${EDGE[@]}"; do
@@ -262,11 +205,7 @@ for ((row=0; row<SQUARE; ++row)); do
# 10 is line size
((c = (LENGTH-${#S})/10))
if ((c > 0)); then
echo "border tile match: ${nums[$j]}"
print_tile "$j"
FINAL[$row,$col]="$j"
final+=("${nums[$j]}")
echo "c=$c"
attach_top "$bottom" "$j"
final_add "$row" "$j"
unset "EDGE[$index]"
@@ -277,14 +216,12 @@ for ((row=0; row<SQUARE; ++row)); do
((index++))
done
if ((found==0)); then
printf "NOT FOUND\n"
exit 0
fi
continue
fi
################################## central tiles
l=${FINAL[$((row-1)),$col]}
printf "FINAL[%d,%d]=%d\n" "$((col-1))" "${FINAL[$,$((col-1))]}"
bottom bottom "${strings[$l]}"
index=0
for j in "${CENTRAL[@]}"; do
@@ -294,11 +231,7 @@ for ((row=0; row<SQUARE; ++row)); do
# 10 is line size
((c = (LENGTH-${#S})/10))
if ((c > 0)); then
echo "border tile match: ${nums[$j]}"
print_tile "$j"
FINAL[$row,$col]="$j"
final+=("${nums[$j]}")
echo "c=$c"
attach_top "$bottom" "$j"
final_add "$row" "$j"
unset "CENTRAL[$index]"
@@ -309,25 +242,12 @@ for ((row=0; row<SQUARE; ++row)); do
((index++))
done
if ((found==0)); then
printf "NOT FOUND\n"
exit 0
fi
continue
done
done
for ((r=0; r<SQUARE; ++r)); do
for ((c=0; c<SQUARE; ++c)); do
k=${FINAL[$r,$c]}
printf "%6d" "${nums[$k]}"
done
printf "\n"
done
print_final
trim_borders
print_final
# dragon:
# 01234567890123456789
# 0 #
@@ -336,18 +256,15 @@ print_final
find_dragons() {
local drag l1 l2 l3
local -i found=0 r c len
printf "square=%d\n" "$SQUARE"
len=${#finalfoo[@]}
len=${#final[@]}
for ((r=0; r<len-2; ++r)); do
l1=${finalfoo[$r]}
l2=${finalfoo[$((r+1))]}
l3=${finalfoo[$((r+2))]}
l1=${final[$r]}
l2=${final[$((r+1))]}
l3=${final[$((r+2))]}
for ((c=0; c<len-20; ++c)); do
drag=${l1:$c+18:1}${l2:$c:1}${l2:$c+5:2}${l2:$c+11:2}${l2:$c+17:3}
drag+=${l3:$c+1:1}${l3:$c+4:1}${l3:$c+7:1}${l3:$c+10:1}${l3:$c+13:1}${l3:$c+16:1}
printf "dragon=%s len=%d\n" "$drag" "${#drag}"
if [[ "$drag" == '###############' ]]; then
printf "found dragon at (%d,%d)\n" "$r" "$c"
((found++))
fi
done
@@ -355,240 +272,29 @@ find_dragons() {
return $found
}
printf "\n"
for k in "${!finalfoo[@]}"; do
printf "%s\n" "${finalfoo[$k]}"
done
for ((i=0; i<4; ++i)); do
find_dragons
found=$?
((found>0)) && break
fliph_final
printf "FLIPH_DRAG\n"
for k in "${!finalfoo[@]}"; do
printf "%s\n" "${finalfoo[$k]}"
done
find_dragons
found=$?
((found>0)) && break
fliph_final
flipv_final
printf "FLIPV_DRAG\n"
for k in "${!finalfoo[@]}"; do
printf "%s\n" "${finalfoo[$k]}"
done
find_dragons
found=$?
((found>0)) && break
flipv_final
rotate_final
printf "ROTATE_DRAG\n"
for k in "${!finalfoo[@]}"; do
printf "%s\n" "${finalfoo[$k]}"
done
done
fullstr="${finalfoo[*]}"
fullstr="${final[*]}"
fullstr="${fullstr//[. ]}"
sharp=${#fullstr}
printf "found=%d \n" $((sharp - found*15))
printf "%s res=%d \n" "$CMD" $((sharp - found*15))
exit 0
###################################################### testing
# printf "corner[0]=%d\n" "${nums[$i]}"
# printf "origin:\n"
# print_tile "$i"
# flip_h "$i"
# printf "horizontal flip:\n"
# print_tile "$i"
# flip_h "$i"
# flip_v "$i"
# printf "vertical flip:\n"
# print_tile "$i"
# flip_v "$i"
# printf "origin:\n"
# print_tile "$i"
# r90 "$i"
# printf "r90:\n"
# print_tile "$i"
# #for i in "${CORNER[@]}"; do
# t=${T[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "T %s: %d\n" "$t" "$count"
# t=${R[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "R %s: %d\n" "$t" "$count"
# t=${B[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "B %s: %d\n" "$t" "$count"
# t=${L[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "L %s: %d\n" "$t" "$count"
# t=${RT[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "RT %s: %d\n" "$t" "$count"
# t=${RR[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "RR %s: %d\n" "$t" "$count"
# t=${RB[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "RB %s: %d\n" "$t" "$count"
# t=${RL[$i]}; S=${ALL//$t}; ((count=(ALLSIZE-${#S})/10))
# printf "RL %s: %d\n" "$t" "$count"
# exit 0
# for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]} \
# ${RT[$i]} ${RR[$i]} ${RB[$i]} ${RL[$i]}; do
# S=${ALL//$t}
# ((count=(ALLSIZE-${#S})/10))
# printf "%s: %d\n" "$t" "$count"
# done
# echo
# exit 0
# r90 "$i"
# printf "r90:\n"
# print_tile "$i"
# r90 "$i"
# r90 "$i"
# r90 "$i"
# printf "origin:\n"
# print_tile "$i"
# r180 "$i"
# printf "r180:\n"
# print_tile "$i"
# r180 "$i"
# printf "origin:\n"
# print_tile "$i"
# r270 "$i"
# printf "r270:\n"
# print_tile "$i"
# r90 "$i"
# printf "origin:\n"
# print_tile "$i"
#test start
# key=1
# str=(${strings[$key]})
# top1="${str[0]}"
# bottom1="${str[9]}"
# declare top bottom left right
# unset left1 right1
# for ((i=0; i<10; ++i)); do
# left1+=${str[$i]:0:1}
# right1+=${str[$i]: -1:1}
# done
# top top ${str[@]}
# bottom bottom ${str[@]}
# right right ${str[@]}
# left left ${str[@]}
# print_tile 1
# printf "T=%s\n" "$top1"
# printf "T=%s\n" "$top"
# echo
# printf "R=%s\n" "$right1"
# printf "R=%s\n" "$right"
# echo
# printf "B=%s\n" "$bottom1"
# printf "B=%s\n" "$bottom"
# echo
# printf "L=%s\n" "$left1"
# printf "L=%s\n" "$left"
# echo
# for ((i=9; i>=0; --i)); do
# fliptop1+=${top:$i:1}
# flipbottom1+=${bottom:$i:1}
# done
# flip fliptop $top
# flip flibottom $bottom
# printf "T=%s\n" "$fliptop1"
# printf "T=%s\n" "$fliptop"
# echo
# exit 0
#test end
# printf "RIGHT=%s\n" "$right"
# # find next tile
# for j in "${EDGE[@]}"; do
# SIDES="${T[$j]} ${R[$j]} ${B[$j]} ${L[$j]} ${RT[$j]} ${RR[$j]} ${RB[$j]} ${RL[$j]}"
# LENGTH=${#SIDES}
# S=${SIDES//$right}
# # 10 is line size
# ((c = (LENGTH-${#S})/10))
# if ((c > 0)); then
# echo "border tile match: ${nums[$j]}"
# print_tile "$j"
# final+=("${nums[$j]}")
# echo "c=$c"
# attach_left "$right" "$j"
# unset "EDGE[$j]"
# EDGE=("${EDGE[@]}")
# break
# fi
# done
# echo after done
# print_tile "$j"
# right right "${strings[$j]}"
# printf "RIGHT=%s\n" "$right"
# # find next tile
# for j in "${CORNER[@]}"; do
# SIDES="${T[$j]} ${R[$j]} ${B[$j]} ${L[$j]} ${RT[$j]} ${RR[$j]} ${RB[$j]} ${RL[$j]}"
# LENGTH=${#SIDES}
# S=${SIDES//$right}
# # 10 is line size
# ((c = (LENGTH-${#S})/10))
# if ((c > 0)); then
# echo "corner tile match: ${nums[$j]}"
# final+=("${nums[$j]}")
# print_tile "$j"
# echo "c=$c"
# attach_left "$right" "$j"
# unset "CORNER[$j]"
# CORNER=("${CORNER[@]}")
# break
# fi
# done
# exit 0
i=${CORNER[0]}
printf "corner=%d=%d\n" "$i" "${nums[$i]}"
printf "origin:\n"
print_tile "$i"
flip_h "$i"
printf "horizontal flip:\n"
print_tile "$i"
flip_h "$i"
flip_v "$i"
printf "vertical flip:\n"
print_tile "$i"
flip_v "$i"
printf "origin:\n"
print_tile "$i"
declare l="" r=""
right r "${strings[$i]}"
printf "right=%s\n" "$r"
printf "origin:\n"
print_tile "$i"
r=""; l=""
left l "${strings[$i]}"
printf "left =%s\n" "$l"
printf "origin:\n"
print_tile "$i"
echo ZOBI