forked from organix/pijFORTHos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adapted from tests
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
\ Adapted from the forth version at | ||
\ http://rosettacode.org/wiki/N-queens_problem | ||
\ requires jonesforth.f | ||
|
||
VARIABLE SOLUTIONS | ||
VARIABLE NODES | ||
|
||
: BITS ( N -- MASK ) 1 SWAP LSHIFT 1- ; | ||
: LOWBIT ( MASK -- BIT ) DUP NEGATE AND ; | ||
: LOWBIT- ( MASK -- BITS ) DUP 1- AND ; | ||
|
||
: NEXT3 ( DL DR F FILES -- DL DR F DL' DR' F' ) | ||
INVERT >R | ||
2 PICK RSP@ @ AND 2* 1+ | ||
2 PICK RSP@ @ AND 2/ | ||
2 PICK R> AND ; | ||
|
||
: TRY ( DL DR F -- ) | ||
DUP IF | ||
1 NODES +! | ||
DUP 2OVER AND AND | ||
BEGIN ?DUP WHILE | ||
DUP >R LOWBIT NEXT3 RECURSE R> LOWBIT- | ||
REPEAT | ||
ELSE 1 SOLUTIONS +! THEN | ||
DROP 2DROP ; | ||
|
||
: QUEENS ( N -- ) | ||
0 SOLUTIONS ! 0 NODES ! | ||
-1 -1 ROT BITS TRY | ||
SOLUTIONS @ . ." SOLUTIONS, " NODES @ . ." NODES" CR ; | ||
|
||
8 QUEENS \ 92 SOLUTIONS, 1965 NODES |