add tools for tile manipulation, first code for part 2
This commit is contained in:
@@ -6,8 +6,11 @@ CMD=${0##*/}
|
|||||||
set -o noglob
|
set -o noglob
|
||||||
shopt -s extglob
|
shopt -s extglob
|
||||||
|
|
||||||
|
source tile.bash
|
||||||
|
|
||||||
#declare -A strings
|
#declare -A strings
|
||||||
declare -a strings T R B L RT RR RB RL nums
|
declare -a strings T R B L RT RR RB RL nums
|
||||||
|
declare -A final
|
||||||
declare -a CORNER EDGE CENTRAL
|
declare -a CORNER EDGE CENTRAL
|
||||||
declare -i count=-1 res=1
|
declare -i count=-1 res=1
|
||||||
|
|
||||||
@@ -26,28 +29,67 @@ while read -r line; do
|
|||||||
done
|
done
|
||||||
((count--))
|
((count--))
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
declare top bottom left right
|
||||||
|
declare fliptop flipbottom flipleft flipright
|
||||||
|
|
||||||
for key in "${!nums[@]}"; do
|
for key in "${!nums[@]}"; do
|
||||||
# shellcheck disable=SC2206
|
# shellcheck disable=SC2206
|
||||||
str=(${strings[$key]})
|
str=(${strings[$key]})
|
||||||
top="${str[0]}"
|
top top "${str[@]}"
|
||||||
bottom="${str[9]}"
|
right right "${str[@]}"
|
||||||
|
bottom bottom "${str[@]}"
|
||||||
|
left left "${str[@]}"
|
||||||
|
flip fliptop "$top"
|
||||||
|
flip flipright "$right"
|
||||||
|
flip flipbottom "$bottom"
|
||||||
|
flip flipleft "$left"
|
||||||
|
|
||||||
T+=("$top")
|
T+=("$top")
|
||||||
B+=("$bottom")
|
B+=("$bottom")
|
||||||
# find out right and bottom
|
|
||||||
unset left right
|
|
||||||
for ((i=0; i<10; ++i)); do
|
|
||||||
left+=${str[$i]:0:1}
|
|
||||||
right+=${str[$i]: -1:1}
|
|
||||||
done
|
|
||||||
R+=("$right")
|
R+=("$right")
|
||||||
L+=("$left")
|
L+=("$left")
|
||||||
unset fliptop flipbottom flipleft flipright
|
|
||||||
for ((i=9; i>=0; --i)); do
|
|
||||||
fliptop+=${top:$i:1}
|
|
||||||
flipbottom+=${bottom:$i:1}
|
|
||||||
flipleft+=${left:$i:1}
|
|
||||||
flipright+=${right:$i:1}
|
|
||||||
done
|
|
||||||
RT+=("$fliptop")
|
RT+=("$fliptop")
|
||||||
RR+=("$flipright")
|
RR+=("$flipright")
|
||||||
RB+=("$flipbottom")
|
RB+=("$flipbottom")
|
||||||
@@ -71,22 +113,152 @@ for ((i=0; i<${#nums[@]}; ++i)); do
|
|||||||
#printf "%s: %d\n" "${nums[$i]}" "$count"
|
#printf "%s: %d\n" "${nums[$i]}" "$count"
|
||||||
case "$count" in
|
case "$count" in
|
||||||
6)
|
6)
|
||||||
CORNER+=(${nums[$i]})
|
CORNER+=("$i")
|
||||||
((res*=${nums[$i]}))
|
((res*=${nums[$i]}))
|
||||||
;;
|
;;
|
||||||
7)
|
7)
|
||||||
EDGE+=(${nums[$i]})
|
EDGE+=("$i")
|
||||||
;;
|
;;
|
||||||
8)
|
8)
|
||||||
CENTRAL+=(${nums[$i]})
|
CENTRAL+=("$i")
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
printf "CENTRAL[%d]=%s\n" "${#CENTRAL[@]}" "${CENTRAL[*]}"
|
printf "CENTRAL[%d]=%s\n" "${#CENTRAL[@]}" "${CENTRAL[*]}"
|
||||||
printf "EDGE[%d]=%s\n" "${#EDGE[@]}" "${EDGE[*]}"
|
printf "EDGE[%d]=%s\n" "${#EDGE[@]}" "${EDGE[*]}"
|
||||||
printf "CORNER[%d]=%s\n" "${#CORNER[@]}" "${CORNER[*]}"
|
printf "CORNER[%d]=%s\n" "${#CORNER[@]}" "${CORNER[*]}"
|
||||||
printf "%s : res=%d\n" "$CMD" "$res"
|
printf "%s : res=%d\n" "$CMD" "$res"
|
||||||
|
|
||||||
|
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=""
|
||||||
|
rightcol r "${strings[$i]}"
|
||||||
|
printf "right=%s\n" "$r"
|
||||||
|
printf "origin:\n"
|
||||||
|
print_tile "$i"
|
||||||
|
r=""; l=""
|
||||||
|
leftcol l "${strings[$i]}"
|
||||||
|
printf "left =%s\n" "$l"
|
||||||
|
printf "origin:\n"
|
||||||
|
print_tile "$i"
|
||||||
|
|
||||||
|
|
||||||
exit 0
|
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"
|
||||||
|
123
2020/day20/tile.bash
Normal file
123
2020/day20/tile.bash
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
#!/usr/bin/bash
|
||||||
|
|
||||||
|
print_tile() {
|
||||||
|
local -i t=$1
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a src=(${strings[$t]})
|
||||||
|
printf "%s\n" "${src[@]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
flip_v() {
|
||||||
|
local -i t=$1 i
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a dst src=(${strings[$t]})
|
||||||
|
for ((i=0; i<10; ++i)); do
|
||||||
|
dst[$((9-i))]=${src[$i]}
|
||||||
|
done
|
||||||
|
strings[$t]="${dst[*]}"
|
||||||
|
}
|
||||||
|
flip_h() {
|
||||||
|
local -i t=$1 i j
|
||||||
|
local str1 str2
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a dst src=(${strings[$t]})
|
||||||
|
local str1 str2
|
||||||
|
for ((i=0; i<10; ++i)); do
|
||||||
|
str1=${src[$i]}
|
||||||
|
str2=""
|
||||||
|
for ((j=9; j>=0; --j)); do
|
||||||
|
str2+=${str1:$j:1}
|
||||||
|
done
|
||||||
|
dst+=("$str2")
|
||||||
|
done
|
||||||
|
strings[$t]="${dst[*]}"
|
||||||
|
}
|
||||||
|
r90() {
|
||||||
|
local -i t=$1 i j
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a dst src=(${strings[$t]}) str2
|
||||||
|
local str1
|
||||||
|
for ((i=9; i>=0; --i)); do
|
||||||
|
str1=${src[$i]}
|
||||||
|
for ((j=0; j<10; ++j)); do
|
||||||
|
str2[$j]+=${str1:$j:1}
|
||||||
|
done
|
||||||
|
#dst+=("$str2")
|
||||||
|
done
|
||||||
|
strings[$t]="${str2[*]}"
|
||||||
|
|
||||||
|
}
|
||||||
|
r180() {
|
||||||
|
local -i t=$1 i j
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a dst src=(${strings[$t]})
|
||||||
|
local str1 str2
|
||||||
|
for ((i=9; i>=0; --i)); do
|
||||||
|
str1=${src[$i]}
|
||||||
|
str2=""
|
||||||
|
for ((j=9; j>=0; --j)); do
|
||||||
|
str2+=${str1:$j:1}
|
||||||
|
done
|
||||||
|
dst+=("$str2")
|
||||||
|
done
|
||||||
|
strings[$t]="${dst[*]}"
|
||||||
|
}
|
||||||
|
r270() {
|
||||||
|
local -i t=$1 i j
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a dst src=(${strings[$t]}) str2
|
||||||
|
local str1
|
||||||
|
for ((i=0; i<10; ++i)); do
|
||||||
|
str1=${src[$i]}
|
||||||
|
for ((j=0; j<10; ++j)); do
|
||||||
|
str2[$j]+=${str1:9-$j:1}
|
||||||
|
done
|
||||||
|
#dst+=("$str2")
|
||||||
|
done
|
||||||
|
strings[$t]="${str2[*]}"
|
||||||
|
}
|
||||||
|
left() {
|
||||||
|
local -n _r="$1"
|
||||||
|
local -i i
|
||||||
|
shift
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a tile=($*)
|
||||||
|
_r=""
|
||||||
|
for ((i=0; i<10; ++i)); do
|
||||||
|
_r+=${tile[$i]:0:1}
|
||||||
|
done
|
||||||
|
}
|
||||||
|
right() {
|
||||||
|
local -n _r="$1"
|
||||||
|
local -i i
|
||||||
|
shift
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a tile=($*)
|
||||||
|
_r=""
|
||||||
|
for ((i=0; i<10; ++i)); do
|
||||||
|
_r+=${tile[$i]: -1:1}
|
||||||
|
done
|
||||||
|
}
|
||||||
|
top() {
|
||||||
|
local -n _r="$1"
|
||||||
|
shift
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a tile=($*)
|
||||||
|
_r=${tile[0]}
|
||||||
|
}
|
||||||
|
bottom() {
|
||||||
|
local -n _r="$1"
|
||||||
|
shift
|
||||||
|
# shellcheck disable=SC2206
|
||||||
|
local -a tile=($*)
|
||||||
|
_r=${tile[9]}
|
||||||
|
}
|
||||||
|
flip() {
|
||||||
|
local -n _r="$1"
|
||||||
|
local str="$2"
|
||||||
|
local -i i
|
||||||
|
_r=""
|
||||||
|
for ((i=9; i>=0; --i)); do
|
||||||
|
_r+=${str:$i:1}
|
||||||
|
done
|
||||||
|
}
|
Reference in New Issue
Block a user