Skip to content

Instantly share code, notes, and snippets.

@masak
Created May 4, 2009 10:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save masak/106418 to your computer and use it in GitHub Desktop.
== 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