interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / ordered-partitions / ordered-partitions.om

115 lines · 2089 bytes Raw Blame History
1note Rosetta Code task "Ordered partitions": https://rosettacode.org/wiki/Ordered_partitions
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tlp "("
8text trp ")"
9text tlb "{"
10text trb "}"
11text tcomma ", "
12text tpart "partitions"
13global size ulong 4
14global owner ulong 8
15global nb ulong 1
16global nel ulong 1
17note emit writes every byte of a to stdout; a short write is retried, an error exits 1
18fn emit a view uchar * to void
19o = ulong 0
20while lt o len a bound 4096
21k = write 1 a o sub len a o
22if le k 0
23exit 1
24end
25o = add o ulong k
26end
27end
28note emitu writes n in decimal; 20 digits hold every ulong
29fn emitu n ulong to void
30d = new uchar 20 0
31i = ulong 20
32v = n
33while true bound 20
34i = sub i 1
35put d i uchar add rem v 10 48
36v = div v 10
37if eq v 0
38break
39end
40end
41emit part d i sub 20 i
42end
43note owner[i] is the block holding element i+1 (nb = unassigned); blocks are filled in order, each with
44note its elements chosen as a lexicographic combination of what is left, so the output is in order
45fn show to void
46emit tlp
47for b ulong get nb 0
48if gt b 0
49emit tcomma
50end
51emit tlb
52first = true
53for i ulong get nel 0
54if eq get owner i b
55if not first
56emit tcomma
57end
58emitu add i 1
59first = false
60end
61end
62emit trb
63end
64emit trp
65emit nl
66end
67note fill starts block b, or prints the partition once every block is full
68fn fill b ulong to void
69if eq b get nb 0
70show
71ret
72end
73choose b get size b 0
74end
75note choose gives block b `need` more elements, each larger than the last one chosen (at index >= from)
76fn choose b ulong need ulong from ulong to void
77if eq need 0
78fill add b 1
79ret
80end
81n = get nel 0
82for x ulong sub n from
83i = add from x
84if eq get owner i get nb 0
85put owner i b
86choose b sub need 1 add i 1
87put owner i get nb 0
88end
89end
90end
91fn run s0 ulong s1 ulong s2 ulong to void
92put nb 0 3
93put size 0 s0
94put size 1 s1
95put size 2 s2
96put nel 0 add add s0 s1 s2
97for i ulong get nel 0
98put owner i 3
99end
100emit tpart
101emit tlp
102emitu s0
103emit tcomma
104emitu s1
105emit tcomma
106emitu s2
107emit trp
108emit nl
109fill 0
110end
111fn main to int
112run 2 0 2
113run 1 2 1
114ret 0
115end