PATH=/bin:/usr/bin if test $# -eq 0 then echo usage: nimmove nsticks ... >&2 exit 1 fi # compute 'b', the XOR of all piles b=0 for p do binp=`echo $p 2o p | dc` b=`expr $b + $binp | tr 2 0` done # strategy depends on whether the big XOR is zero if test $b -eq 0 then # there is no good move takesticks=1 pileno=0 for p do pileno=`expr $pileno + 1` if test $p -gt 0 then frompile=$pileno fi done else # find a pile for which nsticks XOR b is less than nsticks pileno=0 for p do pileno=`expr $pileno + 1` binp=`echo $p 2o p | dc` new=`(echo 2i; expr $binp + $b | tr 2 0; echo p) | dc` if test $new -lt $p then takesticks=`expr $p - $new` frompile=$pileno fi done fi echo I take $takesticks sticks from pile $frompile