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-unbounded.wiki

99 lines · 2167 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 thead "panacea ichor gold value weight volume\n"
10text ttenth "/10 "
11text tthou "/1000\n"
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note the weights are in tenths (0.3, 0.2, 2.0 -> 3, 2, 20; limit 25 -> 250) and the volumes in
39note thousandths (0.025, 0.015, 0.002 -> 25, 15, 2; limit 0.25 -> 250), so all arithmetic is exact;
40note values are 3000, 1800 and 2500; every count that fits is tried (at most 11 * 17 * 13 cases)
41fn fits a ulong b ulong c ulong to bool
42w = add add mul a 3 mul b 2 mul c 20
43v = add add mul a 25 mul b 15 mul c 2
44ret and le w 250 le v 250
45end
46fn value a ulong b ulong c ulong to ulong
47ret add add mul a 3000 mul b 1800 mul c 2500
48end
49fn row a ulong b ulong c ulong to void
50emitu a
51emit sp
52emitu b
53emit sp
54emitu c
55emit sp
56emitu value a b c
57emit sp
58emitu add add mul a 3 mul b 2 mul c 20
59emit ttenth
60emitu add add mul a 25 mul b 15 mul c 2
61emit tthou
62end
63note pass 1 finds the best value, pass 2 prints every combination that reaches it
64fn main to int
65best = ulong 0
66for a ulong 11
67for b ulong 17
68for c ulong 13
69if fits a b c
70if gt value a b c best
71best = value a b c
72end
73end
74end
75end
76end
77emit thead
78for a ulong 11
79for b ulong 17
80for c ulong 13
81if fits a b c
82if eq value a b c best
83row a b c
84end
85end
86end
87end
88end
89ret 0
90end
91</syntaxhighlight>
92{{out}}
93<pre>
94panacea ichor gold value weight volume
950 15 11 54500 250/10 247/1000
963 10 11 54500 249/10 247/1000
976 5 11 54500 248/10 247/1000
989 0 11 54500 247/10 247/1000
99</pre>