RosettaCodeData/Task/N-queens-problem/Tailspin/n-queens-problem-1.tailspin

32 lines
927 B
Plaintext

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