Non-transitive dice: Difference between revisions

→‎{{header|Raku}}: minor efficiency tweaks
(→‎{{header|Raku}}: generalization and a bit faster)
(→‎{{header|Raku}}: minor efficiency tweaks)
Line 1,242:
 
sub FaceCombs(\N, @dp) { # for brevity, changed to use 0-based dice on input
my @res = my @found = [];
for [X] @dp {
unless @found[ my \key = [+] ( N «**« (N-1…0) ) Z* $_.sort ] {
@found[key] = True;
@res.push: $_ »+» 1
}
}
return @res
}
 
sub infix:<⚖️>(@x, @y) {
my $xw\b = my $yw(@x X<=> 0@y).Bag;
+(b{Less} <=> b{More})
for @x X<=> @y { given $_.Int { when 1 {$xw++} ; when -1 {$yw++} } }
return ( $xw <=> $yw ).Int
}
 
sub findIntransitive(\N, \cs) {
my @res = [];
race for [X] ^+cs xx N -> @die {
my $skip = False;
for @die[0..*].rotor(2 => -1) -> @p {
{ $skip = True and last } unless cs[@p[0]] ⚖️ cs[@p[1]] == -1
}
next if $skip;
if cs[@die[0]] ⚖️ cs[@die[*-1]] == 1 { @res.push: [ cs[@die[0..*]] ] }
}
return @res
}
 
say "Number of eligible 4-faced dice : ", +(my \combs = FaceCombs(4,@dicepool));
for 3, 4 {
my @output = findIntransitive($_, combs);
say +@output, " ordered lists of $_ non-transitive dice found, namely:";
.say for @output;
}</lang>
{{out}}
10,327

edits