2022 day 12 Bash final version
This commit is contained in:
@@ -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
|
||||
|
@@ -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 "$@"
|
||||
|
Reference in New Issue
Block a user