From efe0dec8f0238758b9f19f755603f5da45fdd660 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Thu, 22 Dec 2022 21:38:26 +0100 Subject: [PATCH] 2022 day 12 Bash final version --- 2022/RESULTS.txt | 14 ++++++++++++ 2022/day12/aoc.bash | 56 +-------------------------------------------- 2 files changed, 15 insertions(+), 55 deletions(-) diff --git a/2022/RESULTS.txt b/2022/RESULTS.txt index 91e6169..6a936ab 100644 --- a/2022/RESULTS.txt +++ b/2022/RESULTS.txt @@ -251,3 +251,17 @@ aoc.bash: res=13119526120 aoc-c: res=13119526120 time: 0:00.01 real, 0.01 user, 0.00 sys context-switch: 13+1, page-faults: 0+87 + +========================================= +================= day12 ================= +========================================= + ++++++++++++++++++ part 1 +aoc.bash: res=408 + time: 0:00.81 real, 0.80 user, 0.00 sys + context-switch: 223+1, page-faults: 0+486 + ++++++++++++++++++ part 2 +aoc.bash: res=399 + time: 0:00.57 real, 0.56 user, 0.00 sys + context-switch: 35+1, page-faults: 0+459 diff --git a/2022/day12/aoc.bash b/2022/day12/aoc.bash index 2288d90..49a3624 100755 --- a/2022/day12/aoc.bash +++ b/2022/day12/aoc.bash @@ -42,54 +42,34 @@ printvis() { push1() { # push part 1 local -i _d="$1" _x="$2" _y="$3" _h="$4" local _c="$_x,$_y" - printf "push d=%d x=%d y=%d h=%d nh=%d... " "$_d" "$_x" "$_y" "$_h" \ - "${map[$_c]}" if (( !visited[$_c] && map[$_c] <= (_h + 1) )); then (( _d++ )) visited["$_c"]=$_d queue+=("$_d:$_c") - echo "$_d:$_c" pushed. - else - echo not pushed. fi - #local str="$_d:$_x-$_y" - #queue+=("$1") } push2() { # push part 2 local -i _d="$1" _x="$2" _y="$3" _h="$4" local _c="$_x,$_y" - printf "push d=%d x=%d y=%d h=%d nh=%d... " "$_d" "$_x" "$_y" "$_h" \ - "${map[$_c]}" if (( !visited[$_c] && map[$_c] >= (_h - 1) )); then (( _d++ )) visited["$_c"]=$_d queue+=("$_d:$_c") - echo "$_d:$_c" pushed. - else - echo not pushed. fi - #local str="$_d:$_x-$_y" - #queue+=("$1") } pop() { local -n _d="$1" _x="$2" _y="$3" local head - ((!${#queue[@]})) && echo pop: queue empty. && return 1 + ((!${#queue[@]})) && echo pop: queue empty. && exit 1 head="${queue[0]}" - #echo "Q=${#queue[@]}=${queue[*]} head=$head" - # shellcheck disable=2034 - printf "pop: %s\t" "$head" _d=${head%:*} - # shellcheck disable=2034 head=${head#*:} _x=${head%,*} _y=${head#*,} unset 'queue[0]' queue=("${queue[@]}") - printf "pop: d=%d x=%d y=%d remain=%d\n" "$_d" "$_x" "$_y" "${#queue[@]}" - #echo "Q=${#queue[@]}=${queue[*]} head=$head" return 0 } @@ -98,7 +78,6 @@ init() { declare -i i=1 for c in {a..z}; do (( height[$c] = i++ )) - #echo done height[S]=1 height[E]=26 @@ -115,53 +94,36 @@ parse() { readarray -t _map X=${#_map[0]} Y=${#_map[@]} - echo "X=$X Y=$Y" # create array map[x,y] for (( y = 0; y < Y; ++y )); do for (( x = 0; x < X; ++x )); do c=${_map[$y]:x:1} - # printf "y=%d x=%d c=%s h=%d " "$y" "$x" "$c" "${height[$c]}" map["$x,$y"]=${height[$c]} - # printf "M=%s\n" "${map["$x,$y"]}" case "$c" in S) start="$x,$y" ((part == 1)) && $push 0 "$x" "$y" 1 - #printf "\tstart\n" ;; E) end="$x,$y" ((part == 2)) && $push 0 "$x" "$y" 1 - #printf "\tend\n" ;; esac done done - printf "start=%s end=%s\n" "$start" "$end" - printmap } solve() { - #local depth coord local -i h d x y - - #push 0 ${start%-*} ${start#*-} 1 - #push "11:46-55" while pop d x y; do (( h=${map["$x,$y"]} )) if [[ $part == 1 ]]; then if [[ "$x,$y" == "$end" ]]; then res=$((d-1)) - printmap - echo - printvis return fi else if [[ "${map["$x,$y"]}" == 1 ]]; then res=$((d-1)) - printmap - echo - printvis return fi fi @@ -177,23 +139,7 @@ solve() { if ((x > 0)); then # west $push "$d" "$((x-1))" "$y" "$h" fi - #echo "d=$d x=$x y=$y" - echo done - #push "10:45-54" - # push "11:46-55" - # pop dep x y - # echo "d=$d x=$x y=$y" - # pop dep x y - # echo "d=$d x=$x y=$y" - # push "78:1-2" - # pop dep x y - # echo "d=$d x=$x y=$y" - # #pop d c - # #echo "d=$d c=$c" - - # : - # #(( part == 2 )) && (( _loops = 10000, divisor = 1 )) } main "$@"