# Generates a list of all permutations of the list @candidates # satisfying the Langford property. sub langford(@candidates, @slots = [0 xx 2*@candidates]) { return [@slots] if all @slots; my @found; for @candidates -> $c { for @slots[0..@slots-$c-2].kv -> $k, $v { if !$v { if !@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;