interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / knapsack-problem-0-1 / knapsack-problem-0-1.om

142 lines · 3044 bytes Raw Blame History
1note Rosetta Code task "Knapsack problem/0-1": https://rosettacode.org/wiki/Knapsack_problem/0-1
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text 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"
8text 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"
9text tw "total weight "
10text tv "total value "
11global vals long 44
12global best long 401
13global keep uchar 9223
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41fn emiti n long to void
42if lt n 0
43emit minus
44emitu add bitcast ulong not n 1
45else
46emitu ulong n
47end
48end
49note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
50note returns how many; one pass over s, so the loop is bounded by len s
51fn parse s view uchar * to ulong
52n = ulong 0
53v = long 0
54sg = long 1
55dig = false
56for i ulong len s
57c = get s i
58if eq c 45
59sg = long -1
60else
61if and ge c 48 le c 57
62v = add mul v 10 long sub c 48
63dig = true
64else
65if dig
66put vals n mul sg v
67n = add n 1
68end
69v = long 0
70sg = long 1
71dig = false
72end
73end
74end
75if dig
76put vals n mul sg v
77n = add n 1
78end
79ret n
80end
81note item writes the k-th name of the |-separated names text
82fn item k ulong to void
83f = ulong 0
84st = ulong 0
85for i ulong len names
86if eq get names i 124
87if eq f k
88emit part names st sub i st
89ret
90end
91f = add f 1
92st = add i 1
93end
94end
95emit part names st sub len names st
96end
97note pack runs the 0-1 dynamic program over capacities 0..400: best[c] is the best value with weight <= c
98note using the items seen so far; keep[i*401+c] records that item i was taken at capacity c
99fn pack n ulong to void
100for i ulong n
101w = ulong get vals mul i 2
102v = get vals add mul i 2 1
103for x ulong 401
104c = sub 400 x
105if ge c w
106if gt add get best sub c w v get best c
107put best c add get best sub c w v
108put keep add mul i 401 c 1
109end
110end
111end
112end
113end
114fn main to int
115n = div parse data 2
116pack n
117note walk back from capacity 400 over the items in reverse, taking each recorded item
118c = ulong 400
119tot = ulong 0
120for x ulong n
121i = sub sub n x 1
122if eq get keep add mul i 401 c 1
123put vals add mul i 2 1 -1
124w = ulong get vals mul i 2
125c = sub c w
126tot = add tot w
127end
128end
129for i ulong n
130if eq get vals add mul i 2 1 -1
131item i
132emit nl
133end
134end
135emit tw
136emitu tot
137emit nl
138emit tv
139emiti get best 400
140emit nl
141ret 0
142end