interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / n-queens-problem / n-queens-problem.om

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