Non-transitive dice: Difference between revisions
Content added Content deleted
(→{{header|Raku}}: generalization and a bit faster) |
Thundergnat (talk | contribs) (→{{header|Raku}}: minor efficiency tweaks) |
||
Line 1,242: | Line 1,242: | ||
sub FaceCombs(\N, @dp) { # for brevity, changed to use 0-based dice on input |
sub FaceCombs(\N, @dp) { # for brevity, changed to use 0-based dice on input |
||
my @res = my @found = []; |
my @res = my @found = []; |
||
for [X] @dp { |
for [X] @dp { |
||
unless @found[ my \key = [+] ( N «**« (N-1…0) ) Z* |
unless @found[ my \key = [+] ( N «**« (N-1…0) ) Z* .sort ] { |
||
@found[key] = True; |
@found[key] = True; |
||
@res.push: $_ »+» 1 |
@res.push: $_ »+» 1 |
||
} |
} |
||
} |
} |
||
@res |
|||
} |
} |
||
sub infix:<⚖️>(@x, @y) { |
sub infix:<⚖️>(@x, @y) { |
||
my |
my \b = (@x X<=> @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) { |
sub findIntransitive(\N, \cs) { |
||
my @res = []; |
my @res = []; |
||
for [X] ^+cs xx N -> @die { |
race for [X] ^+cs xx N -> @die { |
||
my $skip = False; |
my $skip = False; |
||
for @die[0..*].rotor(2 => -1) -> @p { |
for @die[0..*].rotor(2 => -1) -> @p { |
||
{ $skip = True and last } unless cs[@p[0]] ⚖️ cs[@p[1]] == -1 |
{ $skip = True and last } unless cs[@p[0]] ⚖️ cs[@p[1]] == -1 |
||
} |
} |
||
next if $skip; |
next if $skip; |
||
if cs[@die[0]] ⚖️ cs[@die[*-1]] == 1 { @res.push: [ cs[@die[0..*]] ] } |
if cs[@die[0]] ⚖️ cs[@die[*-1]] == 1 { @res.push: [ cs[@die[0..*]] ] } |
||
} |
} |
||
@res |
|||
} |
} |
||
say "Number of eligible 4-faced dice : ", +(my \combs = FaceCombs(4,@dicepool)); |
say "Number of eligible 4-faced dice : ", +(my \combs = FaceCombs(4,@dicepool)); |
||
for 3, 4 { |
for 3, 4 { |
||
my @output = findIntransitive($_, combs); |
my @output = findIntransitive($_, combs); |
||
say +@output, " ordered lists of $_ non-transitive dice found, namely:"; |
say +@output, " ordered lists of $_ non-transitive dice found, namely:"; |
||
.say for @output; |
.say for @output; |
||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |