This file contains 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
| == Attempt 1 | |
| sub all-possible-orderings(@a, @prefix=[]) { | |
| return [@prefix] unless @a.elems; | |
| return gather for @a.kv -> $k, $v { | |
| my @others = @a[0..^$k, $k^..^*]; | |
| take all-possible-orderings(@others, [@prefix.values, $v]) | |
| } | |
| } | |
| sub langford(@a) { | |
| for 1..(@a/2) -> $n { | |
| for @a.kv -> $k1, $v1 { | |
| if $v1 == $n { | |
| for @a[$k1^..^*].kv -> $k2, $v2 { | |
| if $v2 == $n { | |
| return False if $k2 != $n | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return True | |
| }; | |
| say $_.join() for all-possible-orderings([1,1,2,2,3,3]).grep({ langford($_) }).uniq | |
| == Attempt 2 | |
| sub langford(@candidates, @slots = [0 xx 2*@candidates]) { | |
| return [@slots] unless @slots.grep: { !$_ }; | |
| my @found; | |
| for @candidates -> $c { | |
| for @slots[0..@slots-$c-2].kv -> $k, $v { | |
| if !$v && !@slots[$k+$c+1] { | |
| my @new-slots = @slots; @new-slots[$k, $k+$c+1] = $c, $c; | |
| push @found, | |
| langford((grep { $_ != $c }, @candidates), | |
| @new-slots); | |
| last; | |
| } | |
| } | |
| } | |
| return @found; | |
| } | |
| .join.say for langford(1..4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment