RosettaCodeData/Task/N-queens-problem/Tailspin/n-queens-problem-4.tailspin
2024-11-04 21:53:44 -08:00

52 lines
1.6 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

queens templates
done requires <|=1>
failed requires <|=0>
n is $;
getRowColumn templates
when <|?($@queens(freeRows:; $(r:)::raw) matches <|=0>)> do 0 !
when <|?($@queens(freeMaxs:; $(r:)::raw + $(c:)::raw) matches <|=0>)> do 0 !
when <|?($@queens(freeMins:; $(c:)::raw - $(r:)::raw + $n) matches <|=0>)> do 0 !
otherwise 1!
end getRowColumn
setRowColumn sink
p is $;
@queens(freeRows:; $p(r:)::raw) set $p(val:)::raw;
@queens(freeMaxs:; $p(c:)::raw + $p(r:)::raw) set $p(val:)::raw;
@queens(freeMins:; $p(c:)::raw - $p(r:)::raw + $n) set $p(val:)::raw;
end setRowColumn
placeQueen templates
{c: $, r: 1} -> # !
when <|done> do done´1!
when <|{c: <|$n + 1..>}> do done´1!
when <|{r: <|$n + 1..>}> do failed´0 !
when <|?($ -> getRowColumn matches <|=1>)> do
current is $;
@queens(queenRows:; $(r:)::raw) set $(c:);
{$, val: 0} -> !setRowColumn
{c: $(c:)::raw + 1, r: 1} -> # -> templates
when <|done> do done´1!
otherwise
{$current, val: 1} -> !setRowColumn
{c: $current(c:), r: $current(r:)::raw + 1} !
end -> # !
otherwise {c: $(c:), r: $(r:)::raw + 1} -> # !
end placeQueen
@ set { freeRows: [1..$n -> 1],
freeMaxs: [1..$n*2 -> 1],
freeMins: [1..$n*2 -> 1],
queenRows: [1..$n -> -1] };
1 -> placeQueen -> templates
when <|done> do $@queens(queenRows:) !
otherwise 'non-existent'!
end!
end queens
'A solution to the 8 queens problem is $:8 -> queens;
' !
'A solution to the 4 queens problem is $:4 -> queens;
' !
'A solution to the 3 queens problem is $:3 -> queens;
' !