interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / knapsack-problem-0-1.wiki

162 lines · 3323 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text names "map|compass|water|sandwich|glucose|tin|banana|apple|cheese|beer|suntan cream|camera|T-shirt|trousers|umbrella|waterproof trousers|waterproof overclothes|note-case|sunglasses|towel|socks|book"
10text data "9 150 13 35 153 200 50 160 15 60 68 45 27 60 39 40 23 30 52 10 11 70 32 30 24 15 48 10 73 40 42 70 43 75 22 80 7 20 18 12 4 50 30 10"
11text tw "total weight "
12text tv "total value "
13global vals long 44
14global best long 401
15global keep uchar 9223
16note emit writes every byte of a to stdout; a short write is retried, an error exits 1
17fn emit a view uchar * to void
18o = ulong 0
19while lt o len a bound 4096
20k = write 1 a o sub len a o
21if le k 0
22exit 1
23end
24o = add o ulong k
25end
26end
27note emitu writes n in decimal; 20 digits hold every ulong
28fn emitu n ulong to void
29d = new uchar 20 0
30i = ulong 20
31v = n
32while true bound 20
33i = sub i 1
34put d i uchar add rem v 10 48
35v = div v 10
36if eq v 0
37break
38end
39end
40emit part d i sub 20 i
41end
42note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
43fn emiti n long to void
44if lt n 0
45emit minus
46emitu add bitcast ulong not n 1
47else
48emitu ulong n
49end
50end
51note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
52note returns how many; one pass over s, so the loop is bounded by len s
53fn parse s view uchar * to ulong
54n = ulong 0
55v = long 0
56sg = long 1
57dig = false
58for i ulong len s
59c = get s i
60if eq c 45
61sg = long -1
62else
63if and ge c 48 le c 57
64v = add mul v 10 long sub c 48
65dig = true
66else
67if dig
68put vals n mul sg v
69n = add n 1
70end
71v = long 0
72sg = long 1
73dig = false
74end
75end
76end
77if dig
78put vals n mul sg v
79n = add n 1
80end
81ret n
82end
83note item writes the k-th name of the |-separated names text
84fn item k ulong to void
85f = ulong 0
86st = ulong 0
87for i ulong len names
88if eq get names i 124
89if eq f k
90emit part names st sub i st
91ret
92end
93f = add f 1
94st = add i 1
95end
96end
97emit part names st sub len names st
98end
99note pack runs the 0-1 dynamic program over capacities 0..400: best[c] is the best value with weight <= c
100note using the items seen so far; keep[i*401+c] records that item i was taken at capacity c
101fn pack n ulong to void
102for i ulong n
103w = ulong get vals mul i 2
104v = get vals add mul i 2 1
105for x ulong 401
106c = sub 400 x
107if ge c w
108if gt add get best sub c w v get best c
109put best c add get best sub c w v
110put keep add mul i 401 c 1
111end
112end
113end
114end
115end
116fn main to int
117n = div parse data 2
118pack n
119note walk back from capacity 400 over the items in reverse, taking each recorded item
120c = ulong 400
121tot = ulong 0
122for x ulong n
123i = sub sub n x 1
124if eq get keep add mul i 401 c 1
125put vals add mul i 2 1 -1
126w = ulong get vals mul i 2
127c = sub c w
128tot = add tot w
129end
130end
131for i ulong n
132if eq get vals add mul i 2 1 -1
133item i
134emit nl
135end
136end
137emit tw
138emitu tot
139emit nl
140emit tv
141emiti get best 400
142emit nl
143ret 0
144end
145</syntaxhighlight>
146{{out}}
147<pre>
148map
149compass
150water
151sandwich
152glucose
153banana
154suntan cream
155waterproof trousers
156waterproof overclothes
157note-case
158sunglasses
159socks
160total weight 396
161total value 1030
162</pre>