initial commit

This commit is contained in:
mappu 2024-05-19 18:10:39 +12:00
commit 7d7b3ee15d
2 changed files with 105 additions and 0 deletions

25
countdown.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/bash
set -eu
randn() {
# 1-ARG inclusive
seq 1 $1 | shuf | head -n1
}
big() {
echo '10 25 50 75 100' | tr ' ' $'\n' | shuf | head -n1
}
small() {
seq 1 10 | shuf | head -n1
}
main() {
printf "Target: %03d\n" $(randn 999)
printf "Numbers: %d %d %d %d %d %d\n" \
`big` `big` `small` `small` `small` `small`
}
main "$@"

80
solve.sh Executable file
View File

@ -0,0 +1,80 @@
#!/bin/bash
set -eu
main() {
local targ="$1"
shift
local vals="$@"
#echo "solving for $targ using $vals"
for v in $vals ; do
if [[ $v == $targ ]] ; then
echo won
return 0
fi
done
if [[ $# < 2 ]] ; then
#echo "out of options"
return 1
fi
declare -i i
declare -i j
declare -i k
for (( i=1 ; i <= $# ; i++ )) ; do
for (( j=$i ; j <= $# ; j++ )) ; do
if [[ $i == $j ]] ; then
continue
fi
declare -i iv=${!i}
declare -i jv=${!j}
local others=''
for (( k=1 ; k <= $# ; k++ )) ; do
if [[ $i == $k || $j == $k ]] ; then
continue
fi
local kv=${!k}
others="$others $kv"
done
if main $targ $others $(($iv + $jv)) ; then
echo $iv + $jv = $(($iv + $jv))
return 0
fi
if main $targ $others $(($jv - $iv)) ; then
echo $jv - $iv = $(($jv - $iv))
return 0
fi
if main $targ $others $(($iv - $jv)) ; then
echo $iv - $jv = $(($iv - $jv))
return 0
fi
if main $targ $others $(($iv * $jv)) ; then
echo $iv x $jv = $(($iv * $jv))
return 0
fi
if [[ $jv != 0 && $(( $iv % $jv )) == 0 ]] && \
main $targ $others $(($iv / $jv)) ; then
echo $iv / $jv = $(($iv / $jv))
return 0
fi
if [[ $iv != 0 && $(( $jv % $iv )) == 0 ]] && \
main $targ $others $(($jv / $iv)) ; then
echo $jv / $iv = $(($jv / $iv))
return 0
fi
done
done
#echo "no solution"
return 1
}
main "$@"