# # game of picking pairs of stones # our strategy here is simply to search for the biggest chunk # of stones and take the 2 of the middle # my (\$rock-num, \$player-name) = \$*IN.lines; exit say "aborted" unless \$rock-num.defined and \$player-name.defined; my \$is-computer = \$player-name eq "computer"; my @rocks; if \$is-computer { say "computer takes 0,1"; @rocks = 2..\$rock-num-1; } else { say "human to move: " ~ join "," ,^\$rock-num; my (\$a, \$b) = \$*IN.get.split(/\,/); @rocks = \$b+1..\$rock-num-1,^\$a; } loop { my (\$a, \$b) = get-max @rocks; last unless \$a.defined and \$b.defined; \$is-computer .= not; if \$is-computer { say "computer takes \$a,\$b"; } else { say "human to move: " ~ join ",", @rocks.grep({\$_.defined}); (\$a, \$b) = \$*IN.get.split(/\,/); } for ^@rocks -> \$i { undefine @rocks[\$i] if \$a|\$b ~~ @rocks[\$i] }; } say (\$is-computer ?? "computer" !! "human") ~ " wins"; sub get-max ( @array ) { my @numbers = gather for ( max { \$^a.comb.grep("S") > \$^b.comb.grep("S") }, ([~] "S" X @array.map({ \$_ // "X" })).split(/SX/) ).match(/S(\d+)/,:g) -> \$/ { take +\$/[0] }; return @numbers > 1 ?? @numbers[@numbers/2-1,@numbers/2] !! Any; }