-
-
Save timo/f8480b00d88104afe93876c267840c80 to your computer and use it in GitHub Desktop.
antononcube's "good permutations" with timo's performance improvements - multithreaded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
now.DateTime.say; | |
my @good-permutations := [1..9].permutations | |
.grep( -> @p { | |
.elems == 1 && .[0] == 1 given | |
(^9).map( -> $k { | |
(@p.rotate(-$k) >>-<< (1..9)) | |
.grep(0) | |
.head(2) | |
.elems | |
}) | |
.unique.head(2) | |
}).list.cache; | |
.say for @good-permutations.head(10); | |
now.DateTime.say; | |
say @good-permutations.elems; | |
now.DateTime.say; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
now.DateTime.say; | |
my @rotations := (^9).map(-> $k { my int @a = ((1..9).list.rotate($k)) }).list; | |
sub has-exactly-one($list) { | |
my $it := $list.iterator; return ($it.pull-one !=:= IterationEnd && $it.pull-one =:= IterationEnd); | |
} | |
sub has-exactly($list, $value) { | |
my $it := $list.iterator; return ($it.pull-one === $value && $it.pull-one =:= IterationEnd); | |
} | |
my @good-permutations := [1..9].permutations #.hyper | |
.grep( -> @p { | |
has-exactly(@rotations.map(-> @cmp { | |
# options A, B, and C use has-exactly-one here | |
#has-exactly-one( | |
#(^9).map(-> int $_ { 1 if @p[$_] == @cmp[$_] }) | |
#(^9).map(-> int $_ { @p[$_] - @cmp[$_] }).grep(0) | |
#(@p >>-<< @cmp).grep(0) | |
#) | |
# Option D doesn't use has-exactly-one, because it does its own counting / exiting | |
# my int $cnt = 0; for ^9 -> int $i { last if ($cnt++ if @p[$i] == @cmp[$i]) == 2; }; $cnt == 1; | |
# Option E is a little bit faster than D, and therefore the winner | |
my $cnt = 0; for ^9 -> $i { last if ($cnt++ if @p[$i] == @cmp[$i]) == 2; }; $cnt == 1; | |
}).unique, True) | |
}).list.cache; | |
.say for @good-permutations.head(10); | |
now.DateTime.say; | |
say @good-permutations.elems; | |
now.DateTime.say; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment