Non-transitive dice: Difference between revisions

added Raku programming solution
m (→‎{{header|Phix}}: comment -> use new requires() builtin)
(added Raku programming solution)
Line 1,233:
Out[45]: CacheInfo(hits=2148761, misses=1190, maxsize=None, currsize=1190)
</pre>
 
=={{header|Raku}}==
The 4 dice portion took around 28 minutes to run, which is likely due to 1) using parameters on main loops instead of topic list variable, 2) suboptimal treatments on the all-junctions and of course along with sloppy oversights during the translation.
{{trans|Go}}
<lang perl6># 20201225 Raku programming solution
 
my @dicepool = ^4 xx 4 ;
 
sub fourFaceCombs { # for brevity, changed to use 0-based dice on input
my @res = my @found = [];
for [X] @dicepool {
unless @found[ my \key = [+] (64,16,4,1) Z* $_.sort ] {
@found[key] = True;
@res.push: $_ »+» 1
}
}
return @res
}
 
sub infix:<⚖️>(@x, @y) {
my $xw = my $yw = 0;
for @x X<=> @y { given $_.Int { when 1 {$xw++} ; when -1 {$yw++} } }
return ( $xw <=> $yw ).Int
}
 
sub findIntransitive3(\cs) {
my \c = +cs;
my @res = [];
for [X] ^c xx 3 -> (\i,\j,\k) {
if so all ( cs[i] ⚖️ cs[j] , cs[j] ⚖️ cs[k] ) »==» -1 {
if cs[i] ⚖️ cs[k] == 1 { @res.push: [ cs[i], cs[j], cs[k] ] }
}
}
return @res
}
 
sub findIntransitive4(\cs) {
my \c = +cs;
my @res = [];
for [X] ^c xx 4 -> (\i,\j,\k,\l) {
if so all ( cs[i] ⚖️ cs[j] , cs[j] ⚖️ cs[k], cs[k] ⚖️ cs[l] ) »==» -1 {
if cs[i] ⚖️ cs[l] == 1 { @res.push: [ cs[i], cs[j], cs[k], cs[l] ] }
}
}
return @res
}
 
say "Number of eligible 4-faced dice : ", (my \combs = fourFaceCombs).elems;
 
my @it3 = findIntransitive3(combs);
say +@it3, " ordered lists of 3 non-transitive dice found, namely:";
.say for @it3;
 
my @it4 := findIntransitive4(combs);
say +@it4, " ordered lists of 4 non-transitive dice found, namely:";
.say for @it4;</lang>
{{out}}
<pre>Number of eligible 4-faced dice : 35
3 ordered lists of 3 non-transitive dice found, namely:
[(1 1 4 4) (2 2 2 4) (1 3 3 3)]
[(1 3 3 3) (1 1 4 4) (2 2 2 4)]
[(2 2 2 4) (1 3 3 3) (1 1 4 4)]
4 ordered lists of 4 non-transitive dice found, namely:
[(1 1 4 4) (2 2 2 4) (2 2 3 3) (1 3 3 3)]
[(1 3 3 3) (1 1 4 4) (2 2 2 4) (2 2 3 3)]
[(2 2 2 4) (2 2 3 3) (1 3 3 3) (1 1 4 4)]
[(2 2 3 3) (1 3 3 3) (1 1 4 4) (2 2 2 4)]</pre>
 
=={{header|Wren}}==
351

edits