From fbb553645eafae9ceae8a335440e7f7d5c20f4ae Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 28 Jul 2021 16:38:42 +0200 Subject: [PATCH] day 20 part 2 (bash): final version --- 2020/OUTPUT | 10 +- 2020/day20/OUTPUT | 8 +- 2020/day20/ex2.bash | 310 ++----------------------------------------- 2020/day20/tile.bash | 118 ++-------------- 4 files changed, 34 insertions(+), 412 deletions(-) diff --git a/2020/OUTPUT b/2020/OUTPUT index f610ba2..44ccd8d 100644 --- a/2020/OUTPUT +++ b/2020/OUTPUT @@ -448,6 +448,12 @@ ex2.bash : res=412 ================= day20 ================= ========================================= ++++++++++++++++++ ex1 ex1.bash : res=5966506063747 - time: 0:00.54 real, 0.53 user, 0.00 sys - context-switch: 1+1, page-faults: 0+223 + time: 0:00.56 real, 0.56 user, 0.00 sys + context-switch: 4+1, page-faults: 0+224 + ++++++++++++++++++ ex2 +ex2.bash res=1714 + time: 0:02.14 real, 2.05 user, 0.02 sys + context-switch: 1146+1, page-faults: 0+605 diff --git a/2020/day20/OUTPUT b/2020/day20/OUTPUT index 3c22426..43e9157 100644 --- a/2020/day20/OUTPUT +++ b/2020/day20/OUTPUT @@ -1,4 +1,8 @@ ex1.bash : res=5966506063747 - time: 0:00.54 real, 0.53 user, 0.00 sys - context-switch: 1+1, page-faults: 0+223 + time: 0:00.56 real, 0.56 user, 0.00 sys + context-switch: 4+1, page-faults: 0+224 + +ex2.bash res=1714 + time: 0:02.14 real, 2.05 user, 0.02 sys + context-switch: 1146+1, page-faults: 0+605 diff --git a/2020/day20/ex2.bash b/2020/day20/ex2.bash index 440ae97..8b31f5a 100755 --- a/2020/day20/ex2.bash +++ b/2020/day20/ex2.bash @@ -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 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 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 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 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; row0)) && 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 diff --git a/2020/day20/tile.bash b/2020/day20/tile.bash index 4a60ea4..fc9e6e0 100755 --- a/2020/day20/tile.bash +++ b/2020/day20/tile.bash @@ -1,78 +1,28 @@ #!/usr/bin/bash -print_tile() { - local -i t=$1 - # shellcheck disable=SC2206 - local -a src=(${strings[$t]}) - 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=0; --i)); do - line=${finalfoo[$i]} + line=${final[$i]} for ((j=0; j=0; --j)); do str2+=${str1:$j:1} done dst+=("$str2") done - finalfoo=("${dst[@]}") + final=("${dst[@]}") } final_add() { @@ -99,10 +49,7 @@ final_add() { local -a src=(${strings[$2]}) for ((r=1, t=$((start*8)); r<9; ++r, t++)); do - printf "adding tile %d row %d to final %d\n" "$2" "$r" "$t" - printf " %s -> " "${finalfoo[$t]}" - finalfoo[$t]+=${src[$r]:1:8} - printf "%s\n" "${finalfoo[$t]}" + final[$t]+=${src[$r]:1:8} done } @@ -116,7 +63,6 @@ trim_borders() { s=${s#* } s=${s% * } strings[$k]="$s" - #printf "%s\n" "${strings[$k]}" done } @@ -235,73 +181,46 @@ flip() { _r+=${str:$i:1} done } -print_all_borders() { - local k str - local -i i - for ((i=0; i<${#strings[@]}; ++i)); do - top str "${strings[$i]}" - printf "%s\n" "$str" - right str "${strings[$i]}" - printf "%s\n" "$str" - bottom str "${strings[$i]}" - printf "%s\n" "$str" - left str "${strings[$i]}" - printf "%s\n" "$str" - 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" flip_h "$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 @@ -309,52 +228,39 @@ 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" flip_v "$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" }