omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / n-queens-problem / n-queens-problem.om
| 1 | note Rosetta Code task "N-queens problem": https://rosettacode.org/wiki/N-queens_problem |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text t1 " queens: " |
| 6 | text t2 " solutions\n" |
| 7 | global cols uchar 8 |
| 8 | global d1 uchar 15 |
| 9 | global d2 uchar 15 |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note emitu writes n in decimal; 20 digits hold every ulong |
| 22 | fn emitu n ulong to void |
| 23 | d = new uchar 20 0 |
| 24 | i = ulong 20 |
| 25 | v = n |
| 26 | while true bound 20 |
| 27 | i = sub i 1 |
| 28 | put d i uchar add rem v 10 48 |
| 29 | v = div v 10 |
| 30 | if eq v 0 |
| 31 | break |
| 32 | end |
| 33 | end |
| 34 | emit part d i sub 20 i |
| 35 | end |
| 36 | note counts placements for rows r .. n-1; d1 holds the r+c diagonals, d2 the r-c+7 ones |
| 37 | fn place r ulong n ulong to ulong |
| 38 | if eq r n |
| 39 | ret 1 |
| 40 | end |
| 41 | cnt = ulong 0 |
| 42 | for c ulong n |
| 43 | a = add r c |
| 44 | b = add r sub 7 c |
| 45 | if eq get cols c 0 |
| 46 | if eq get d1 a 0 |
| 47 | if eq get d2 b 0 |
| 48 | put cols c 1 |
| 49 | put d1 a 1 |
| 50 | put d2 b 1 |
| 51 | cnt = add cnt place add r 1 n |
| 52 | put cols c 0 |
| 53 | put d1 a 0 |
| 54 | put d2 b 0 |
| 55 | end |
| 56 | end |
| 57 | end |
| 58 | end |
| 59 | ret cnt |
| 60 | end |
| 61 | fn main to int |
| 62 | for i ulong 8 |
| 63 | n = add i 1 |
| 64 | emitu n |
| 65 | emit t1 |
| 66 | emitu place 0 n |
| 67 | emit t2 |
| 68 | end |
| 69 | ret 0 |
| 70 | end |