Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,31 @@
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

View file

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