2015-11-18 06:14:39 +00:00
|
|
|
sub MAIN(\N = 8) {
|
2013-04-10 23:57:08 -07:00
|
|
|
sub collision(@field, $row) {
|
|
|
|
|
for ^$row -> $i {
|
|
|
|
|
my $distance = @field[$i] - @field[$row];
|
2015-11-18 06:14:39 +00:00
|
|
|
return True if $distance == any(0, $row - $i, $i - $row);
|
2013-04-10 23:57:08 -07:00
|
|
|
}
|
2015-11-18 06:14:39 +00:00
|
|
|
False;
|
2013-04-10 23:57:08 -07:00
|
|
|
}
|
2016-12-05 22:15:40 +01:00
|
|
|
sub search(@field, $row) {
|
2015-11-18 06:14:39 +00:00
|
|
|
return @field if $row == N;
|
|
|
|
|
for ^N -> $i {
|
|
|
|
|
@field[$row] = $i;
|
|
|
|
|
return search(@field, $row + 1) || next
|
|
|
|
|
unless collision(@field, $row);
|
2013-04-10 23:57:08 -07:00
|
|
|
}
|
2015-11-18 06:14:39 +00:00
|
|
|
()
|
2013-04-10 23:57:08 -07:00
|
|
|
}
|
2015-11-18 06:14:39 +00:00
|
|
|
for 0 .. N / 2 {
|
|
|
|
|
if search [$_], 1 -> @f {
|
|
|
|
|
say @f;
|
2013-04-10 23:57:08 -07:00
|
|
|
last;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|