tiles re-assembling (tentative)

This commit is contained in:
2021-07-27 18:32:33 +02:00
parent f4348d3faf
commit ba0a6217fd
2 changed files with 526 additions and 167 deletions

View File

@@ -10,9 +10,10 @@ source tile.bash
#declare -A strings
declare -a strings T R B L RT RR RB RL nums
declare -A final
declare -a final
declare -A FINAL
declare -a CORNER EDGE CENTRAL
declare -i count=-1 res=1
declare -i count=-1 res=1 SQUARE
while read -r line; do
case ${line:0:1} in
@@ -27,7 +28,335 @@ while read -r line; do
;;
esac
done
((count--))
((count++))
case "$count" in
9)
SQUARE=3
;;
144)
SQUARE=12
;;
*)
printf "Fatal: unknown square size."
exit 0
;;
esac
printf "size=%d\n" "$SQUARE"
declare top bottom left right
declare fliptop flipbottom flipleft flipright
for key in "${!nums[@]}"; do
# shellcheck disable=SC2206
str=(${strings[$key]})
top top "${str[@]}"
right right "${str[@]}"
bottom bottom "${str[@]}"
left left "${str[@]}"
flip fliptop "$top"
flip flipright "$right"
flip flipbottom "$bottom"
flip flipleft "$left"
T+=("$top")
B+=("$bottom")
R+=("$right")
L+=("$left")
RT+=("$fliptop")
RR+=("$flipright")
RB+=("$flipbottom")
RL+=("$flipleft")
done
ALL="${T[*]} ${R[*]} ${B[*]} ${L[*]} ${RT[*]} ${RR[*]} ${RB[*]} ${RL[*]}"
ALLSIZE=${#ALL}
for ((i=0; i<${#nums[@]}; ++i)); do
c=0
for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]}; do
S=${ALL//$t}
# 10 is line size
((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")
;;
7)
EDGE+=("$i")
;;
8)
CENTRAL+=("$i")
;;
esac
done
printf "CENTRAL[%d]=%s\n" "${#CENTRAL[@]}" "${CENTRAL[*]}"
printf "EDGE[%d]=%s\n" "${#EDGE[@]}" "${EDGE[*]}"
printf "CORNER[%d]=%s\n" "${#CORNER[@]}" "${CORNER[*]}"
printf "%s : count=%d\n" "$CMD" "$count"
for ((row=0; row<SQUARE; ++row)); do
echo ZOBI
for ((col=0; col<SQUARE; ++col)); do
for k in ${!FINAL[@]}; do
printf "final[%s]=%d\n" "$k" "${FINAL[$k]}"
done
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]}
unset "CORNER[0]"
CORNER=("${CORNER[@]}")
printf "corner[0]=%d\n" "${nums[$i]}"
print_tile "$i"; echo
t=${B[$i]}
S=${ALL//$t}
# flip vertical if bottom is a corner side
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 bottom is a corner side
if (( ALLSIZE-${#S} == 10 )); then
echo FLIP_H
flip_h "$i"
fi
print_tile "$i"
FINAL[0,0]="$i"
final+=("${nums[$i]}")
continue
fi
################################## rest of 1st line
if ((row==0)); then
if ((col < SQUARE-1)); then
list=("${EDGE[@]}")
else
list=("${CORNER[@]}")
fi
l=${FINAL[0,$((col-1))]}
printf "FINAL[0,%d]=%d\n" "$((col-1))" "${FINAL[0,$((col-1))]}"
right right "${strings[$l]}"
for j in "${list[@]}"; 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[$row,$col]="$j"
final+=("${nums[$j]}")
echo "c=$c"
attach_left "$right" "$j"
if ((col < SQUARE-1)); then
unset "EDGE[$j]"
EDGE=("${EDGE[@]}")
else
unset "CORNER[$j]"
CORNER=("${CORNER[@]}")
fi
break
fi
done
continue
fi
################################## 1st and last col
if ((col==0 || col==SQUARE-1)); then
if ((row > 0 && row < SQUARE-1)); then
list=("${EDGE[@]}")
else
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"
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"
if ((row < SQUARE-1)); then
unset "EDGE[$j]"
EDGE=("${EDGE[@]}")
else
unset "CORNER[$j]"
CORNER=("${CORNER[@]}")
fi
break
fi
done
continue
fi
################################## 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]}"
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//$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"
unset "EDGE[$j]"
EDGE=("${EDGE[@]}")
break
fi
done
continue
fi
################################## central tiles
l=${FINAL[$((row-1)),$col]}
printf "FINAL[%d,%d]=%d\n" "$((col-1))" "${FINAL[$,$((col-1))]}"
bottom bottom "${strings[$l]}"
for j in "${CENTRAL[@]}"; do
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"
unset "CENTRAL[$j]"
CENTRAL=("${CENTRAL[@]}")
break
fi
done
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
printf "%s \n" "${final[@]}"
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
@@ -71,67 +400,52 @@ done
# exit 0
#test end
declare top bottom left right
declare fliptop flipbottom flipleft flipright
# 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
for key in "${!nums[@]}"; do
# shellcheck disable=SC2206
str=(${strings[$key]})
top top "${str[@]}"
right right "${str[@]}"
bottom bottom "${str[@]}"
left left "${str[@]}"
flip fliptop "$top"
flip flipright "$right"
flip flipbottom "$bottom"
flip flipleft "$left"
T+=("$top")
B+=("$bottom")
R+=("$right")
L+=("$left")
RT+=("$fliptop")
RR+=("$flipright")
RB+=("$flipbottom")
RL+=("$flipleft")
done
ALL="${T[@]} ${R[@]} ${B[@]} ${L[@]} ${RT[@]} ${RR[@]} ${RB[@]} ${RL[@]}"
ALLSIZE=${#ALL}
for ((i=0; i<${#nums[@]}; ++i)); do
count=0
for t in ${T[$i]} ${R[$i]} ${B[$i]} ${L[$i]}; do
# https://stackoverflow.com/questions/26212889/bash-counting-substrings-in-a-string/50601141#50601141
S=${ALL//$t}
# 10 is line size
((count += (ALLSIZE-${#S})/10))
done
# 6 is 4 for itself, + 2 for other matching
#printf "%s: %d\n" "${nums[$i]}" "$count"
case "$count" in
6)
CORNER+=("$i")
((res*=${nums[$i]}))
;;
7)
EDGE+=("$i")
;;
8)
CENTRAL+=("$i")
;;
esac
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
printf "CENTRAL[%d]=%s\n" "${#CENTRAL[@]}" "${CENTRAL[*]}"
printf "EDGE[%d]=%s\n" "${#EDGE[@]}" "${EDGE[*]}"
printf "CORNER[%d]=%s\n" "${#CORNER[@]}" "${CORNER[*]}"
printf "%s : res=%d\n" "$CMD" "$res"
# exit 0
i=${CORNER[0]}
printf "corner=%d=%d\n" "$i" "${nums[$i]}"
printf "origin:\n"
@@ -148,117 +462,13 @@ printf "origin:\n"
print_tile "$i"
declare l="" r=""
rightcol r "${strings[$i]}"
right r "${strings[$i]}"
printf "right=%s\n" "$r"
printf "origin:\n"
print_tile "$i"
r=""; l=""
leftcol l "${strings[$i]}"
left l "${strings[$i]}"
printf "left =%s\n" "$l"
printf "origin:\n"
print_tile "$i"
exit 0
# Choose one corner for top-left, find the correct flip
i=${CORNER[0]}
printf "corner[0]=%d\n" "${nums[$i]}"
print_tile "$i"; echo
t=${B[$i]}
S=${ALL//$t}
# flip vertical if bottom is a corner side
if (( ALLSIZE-${#S} == 10)); then
echo FLIP_V
flip_v "$i"
fi
print_tile "$i"; echo
i=${CORNER[0]}
t=${R[$i]}
S=${ALL//$t}
if (( ALLSIZE-${#S} == 10)); then
echo FLIP_H
flip_h "$i"
fi
print_tile "$i"
final[0,0]="${strings[$i]}"
unset strings[$i]
# find next tile
#for i in ${EDGE[@]}; do
#done
exit 0
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
#done
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"
echo ZOBI

149
2020/day20/tile.bash Normal file → Executable file
View File

@@ -7,6 +7,43 @@ print_tile() {
printf "%s\n" "${src[@]}"
}
print_final() {
local k str
local -i l len r c
local -a tile
str=${strings[0]%% *}
len=${#str}
printf "print_final: len=%d\n" "$len"
for ((r=0; r<SQUARE; ++r)); do
for ((l=0; l<len; ++l)); do # each line
for ((c=0; c<SQUARE; ++c)); do
k=${FINAL[$r,$c]}
# shellcheck disable=SC2206
tile=(${strings[$k]})
str=${tile[$l]}
printf "%s " "$str"
done
printf "\n"
done
printf "\n"
done
}
trim_borders() {
local k s
for k in "${!strings[@]}"; do
s=${strings[$k]}
s=${s//? ?/ }
s=${s#?}
s=${s%?}
s=${s#* }
s=${s% * }
strings[$k]="$s"
#printf "%s\n" "${strings[$k]}"
done
}
flip_v() {
local -i t=$1 i
# shellcheck disable=SC2206
@@ -121,3 +158,115 @@ flip() {
_r+=${str:$i:1}
done
}
# transform $2 tile to match $1 on left
attach_left() {
local _l="$1"
local -i t="$2" m=0
local s
printf "matching left %s for tile:\n" "$_l"
print_tile "$t"
for s in ${T[$t]} ${R[$t]} ${B[$t]} ${L[$t]} \
${RT[$t]} ${RR[$t]} ${RB[$t]} ${RL[$t]}; do
printf "%d: %s\n" "$m" "$s"
if [[ "$s" == "$_l" ]]; then
echo "attach_left: count=$m"
break
fi
((m++))
done
case $m in
0) # top
printf "match=top\n"
r270 "$t"
flip_v "$t"
;;
1) # right
printf "match=right\n"
r180 "$t"
flip_v "$t"
;;
2) # bottom
printf "match=bottom\n"
r90 "$t"
;;
3) # left
printf "match=left\n"
;;
4) # rev top
printf "match=rev top\n"
r270 "$t"
;;
5) # rev right
printf "match=rev right\n"
r180 "$t"
;;
6) # rev bottom
printf "match=rev bottom\n"
r90 "$t"
flip_v "$t"
;;
7) # rev left
printf "match=rev left\n"
flip_v "$t"
;;
esac
echo new orientation:
print_tile "$t"
}
# transform $2 tile to match $1 on left
attach_top() {
local _l="$1"
local -i t="$2" m=0
local s
printf "matching top %s for tile:\n" "$_l"
print_tile "$t"
for s in ${T[$t]} ${R[$t]} ${B[$t]} ${L[$t]} \
${RT[$t]} ${RR[$t]} ${RB[$t]} ${RL[$t]}; do
printf "%d: %s\n" "$m" "$s"
if [[ "$s" == "$_l" ]]; then
echo "attach_top: count=$m"
break
fi
((m++))
done
case $m in
0) # top
printf "match=top\n"
;;
1) # right
printf "match=right\n"
r270 "$t"
;;
2) # bottom
printf "match=bottom\n"
r180 "$t"
flip_h "$t"
;;
3) # left
printf "match=left\n"
r90 "$t"
flip_h "$t"
;;
4) # rev top
printf "match=rev top\n"
flip_h "$t"
;;
5) # rev right
printf "match=rev right\n"
r270 "$t"
flip_h "$t"
;;
6) # rev bottom
printf "match=rev bottom\n"
r180 "$t"
;;
7) # rev left
printf "match=rev left\n"
r90 "$t"
;;
esac
echo new orientation:
print_tile "$t"
}