RosettaCodeData/Task/N-queens-problem/Tailspin/n-queens-problem-2.tailspin
2024-10-16 18:07:41 -07:00

31 lines
873 B
Text

queens templates
n is $;
addColumn templates
prev is $;
addIfPossible templates
row is $;
minor is $ - $prev::length - 1;
major is $ + $prev::length + 1;
-- If prev is not an array that contains row, send it on...
$prev -> if <~|[<|=$row>]>
-> if <|?($(.. as i; -> $ - $i) matches <~|[<|=$minor>]>)>
-> if <|?($(.. as i; -> $ + $i) matches <~|[<|=$major>]>)>
-> [ $..., $row] !
end addIfPossible
1..$n -> addIfPossible !
end addColumn
1..$n -> [$] -> # !
when <|[](=$n)> do $ !
otherwise $ -> addColumn -> # !
end queens
solutions is [ 8 -> queens ];
'For 8 queens there are $solutions::length; solutions
' !
columns is ['abcdefgh'...];
'One of them is $solutions(1) -> $(.. as i; -> '$columns($i);$;');
' !
'For 3 queens there are $:[3 -> queens] -> $::length; solutions
' !