Non-transitive dice: Difference between revisions

Content added Content deleted
(added Raku programming solution)
(→‎{{header|Raku}}: generalization and a bit faster)
Line 1,235: Line 1,235:


=={{header|Raku}}==
=={{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.
The 4 dice portion took around 17 (down from 28) minutes to run .. so it is not done yet.
{{trans|Go}}
{{trans|Go}}
<lang perl6># 20201225 Raku programming solution
<lang perl6># 20201225 Raku programming solution
Line 1,241: Line 1,241:
my @dicepool = ^4 xx 4 ;
my @dicepool = ^4 xx 4 ;


sub fourFaceCombs { # 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] @dicepool {
for [X] @dp {
unless @found[ my \key = [+] (64,16,4,1) Z* $_.sort ] {
unless @found[ my \key = [+] ( N «**« (N-1…0) ) Z* $_.sort ] {
@found[key] = True;
@found[key] = True;
@res.push: $_ »+» 1
@res.push: $_ »+» 1
}
}
}
}
return @res
return @res
}
}


Line 1,258: Line 1,258:
}
}


sub findIntransitive3(\cs) {
sub findIntransitive(\N, \cs) {
my \c = +cs;
my @res = [];
my @res = [];
for [X] ^c xx 3 -> (\i,\j,\k) {
for [X] ^+cs xx N -> @die {
my $skip = False;
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] ] }
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
return @res
}
}


say "Number of eligible 4-faced dice : ", +(my \combs = FaceCombs(4,@dicepool));
sub findIntransitive4(\cs) {
for 3, 4 {
my \c = +cs;
my @res = [];
my @output = findIntransitive($_, combs);
say +@output, " ordered lists of $_ non-transitive dice found, namely:";
for [X] ^c xx 4 -> (\i,\j,\k,\l) {
.say for @output;
if so all ( cs[i] ⚖️ cs[j] , cs[j] ⚖️ cs[k], cs[k] ⚖️ cs[l] ) »==» -1 {
}</lang>
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}}
{{out}}
<pre>Number of eligible 4-faced dice : 35
<pre>Number of eligible 4-faced dice : 35