bash day 22/part 2, final version

This commit is contained in:
2021-08-01 17:57:53 +02:00
parent 12d4bdda61
commit 6e9e4be8e3
3 changed files with 36 additions and 56 deletions

View File

@@ -471,3 +471,17 @@ ex1.bash: res=2211
ex2.bash: res=vv,nlxsmb,rnbhjk,bvnkk,ttxvphb,qmkz,trmzkcfg,jpvz ex2.bash: res=vv,nlxsmb,rnbhjk,bvnkk,ttxvphb,qmkz,trmzkcfg,jpvz
time: 0:01.75 real, 1.74 user, 0.00 sys time: 0:01.75 real, 1.74 user, 0.00 sys
context-switch: 36+4, page-faults: 0+1388 context-switch: 36+4, page-faults: 0+1388
=========================================
================= day22 =================
=========================================
+++++++++++++++++ ex1
ex1.bash: res=31314
time: 0:00.07 real, 0.06 user, 0.00 sys
context-switch: 2+1, page-faults: 0+168
+++++++++++++++++ ex2
ex2.bash: res=32760
time: 1:21.92 real, 81.89 user, 0.01 sys
context-switch: 462+1, page-faults: 0+5135

View File

@@ -1,3 +1,8 @@
ex1.bash: res=31314 ex1.bash: res=31314
time: 0:00.04 real, 0.04 user, 0.00 sys time: 0:00.07 real, 0.06 user, 0.00 sys
context-switch: 0+0, page-faults: 0+166 context-switch: 2+1, page-faults: 0+168
ex2.bash: res=32760
time: 1:21.92 real, 81.89 user, 0.01 sys
context-switch: 462+1, page-faults: 0+5135

View File

@@ -22,89 +22,50 @@ done
# shellcheck disable=SC2206 # shellcheck disable=SC2206
CARDS1=(${cards[0]}) && CARDS2=(${cards[1]}) CARDS1=(${cards[0]}) && CARDS2=(${cards[1]})
# params: (%d)depth (int ref)result (array name)player1 (array name)player2
declare -i game=0
run() { run() {
local -n _res="$1" _p1="$2" _p2="$3" local -n _p1="$1" _p2="$2"
# shellcheck disable=SC2206
local -a p1=(${_p1[@]}) p2=(${_p2[@]}) sp1 sp2 local -a p1=(${_p1[@]}) p2=(${_p2[@]}) sp1 sp2
local -A hash=() local -A hash=()
local winner=0 round=1 g # 1: p1, 2: p2 local -i winner=0 # 1: p1, 2: p2
local str
((game++))
g=$game
printf "\n=== Game %d ===\n" "$g"
#printf " p1=%s \n" "${p1[*]}"
#printf " p2=%s \n" "${p2[*]}"
while ((${#p1[@]} && ${#p2[@]})); do while ((${#p1[@]} && ${#p2[@]})); do
echo
echo -n "-- "
printf "Round %d (Game %d) --\n" "$round" "$g"
S="${p1[*]}"
S="${S// /, }"
printf "Player 1's deck: %s\n" "$S"
S="${p2[*]}"
S="${S// /, }"
printf "Player 2's deck: %s\n" "$S"
str="${p1[*]} ${p2[*]}" str="${p1[*]} ${p2[*]}"
#[[ -v ${!hash[$_str]} ]] && return 1 [[ -v hash["$str"] ]] && return 1
if [[ -v hash["$str"] ]]; then # shellcheck disable=SC2034
_res=1
return 1
fi
printf "Player 1 plays: %s\n" "$p1"
printf "Player 2 plays: %s\n" "$p2"
hash["$str"]="" hash["$str"]=""
if ((p1 < ${#p1[@]} && p2 < ${#p2[@]} )); then if ((p1 < ${#p1[@]} && p2 < ${#p2[@]} )); then
sp1=(${p1[*]:1:p1}) # shellcheck disable=SC2034,SC2206
sp2=(${p2[*]:1:p2}) sp1=(${p1[*]:1:p1}) && sp2=(${p2[*]:1:p2})
printf "Playing a sub-game to determine the winner...\n" run sp1 sp2
run winner sp1 sp2
winner=$? winner=$?
printf "...anyway, back to game %d.\n" "$g"
else else
((p1 > p2)) && winner=1 || winner=2 ((p1 > p2)) && winner=1 || winner=2
fi fi
if ((winner==1)); then if ((winner==1)); then
# shellcheck disable=SC2128
p1+=("$p1" "$p2") p1+=("$p1" "$p2")
_res=1
printf "Player 1 wins round %d of game %d!\n" "$round" "$g"
else else
# shellcheck disable=SC2128
p2+=("$p2" "$p1") p2+=("$p2" "$p1")
_res=2
printf "Player 2 wins round %d of game %d!\n" "$round" "$g"
fi fi
p1=("${p1[@]:1}") p1=("${p1[@]:1}")
p2=("${p2[@]:1}") p2=("${p2[@]:1}")
#printf " p1=%s\n" "${p1[*]}"
#printf " p2=%s\n" "${p2[*]}"
((round++))
done done
_p1=(${p1[@]}) # shellcheck disable=SC2206
_p2=(${p2[@]}) _p1=(${p1[@]}) && _p2=(${p2[@]})
_res="$winner"
printf "The winner of game %d is player %d!\n\n" "$g" "$_res"
return "$winner" return "$winner"
} }
declare -i res run CARDS1 CARDS2
run res CARDS1 CARDS2
declare -i n=$((${#CARDS1[@]}+${#CARDS2[@]})) i declare -i n=$((${#CARDS1[@]}+${#CARDS2[@]})) i
res=0 res=0
S="${CARDS1[*]}"
S="${S// /, }"
printf "Player 1's deck: %s\n" "$S"
S="${CARDS2[*]}"
S="${S// /, }"
printf "Player 2's deck: %s\n" "$S"
for i in "${CARDS1[@]}" "${CARDS2[@]}"; do for i in "${CARDS1[@]}" "${CARDS2[@]}"; do
((res+=n*i, n--)) ((res+=n*i, n--))
done done
printf "%s: res=%d (should be 32760)\n" "$CMD" "$res" printf "%s: res=%d\n" "$CMD" "$res"
exit 0 exit 0