Compare commits

...

2 Commits

Author SHA1 Message Date
f4348d3faf add tools for tile manipulation, first code for part 2 2021-07-27 12:25:43 +02:00
150e4ba2a7 starting code for day 20/part 2 2021-07-25 17:12:36 +02:00
2 changed files with 387 additions and 0 deletions

264
2020/day20/ex2.bash Executable file
View File

@@ -0,0 +1,264 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 20/game 2.
CMD=${0##*/}
set -o noglob
shopt -s extglob
source tile.bash
#declare -A strings
declare -a strings T R B L RT RR RB RL nums
declare -A final
declare -a CORNER EDGE CENTRAL
declare -i count=-1 res=1
while read -r line; do
case ${line:0:1} in
T*)
((count++))
num=${line##* }
num=${num%%:}
nums+=("$num")
;;
\#|.)
strings[$count]="${strings[$count]} $line"
;;
esac
done
((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
# 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
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"
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
# 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
View 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
}