Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/knapsack-problem-0-1.wiki # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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}} 3 Omega has no imports, so the small output helpers (emit and friends) are part of the program. 4 5 om2 6 text nl "\n" 7 text sp " " 8 text minus "-" 9 text 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" 10 text 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" 11 text tw "total weight " 12 text tv "total value " 13 global vals long 44 14 global best long 401 15 global keep uchar 9223 16 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 17 fn emit a view uchar * to void 18 o = ulong 0 19 while lt o len a bound 4096 20 k = write 1 a o sub len a o 21 if le k 0 22 exit 1 23 end 24 o = add o ulong k 25 end 26 end 27 note emitu writes n in decimal; 20 digits hold every ulong 28 fn emitu n ulong to void 29 d = new uchar 20 0 30 i = ulong 20 31 v = n 32 while true bound 20 33 i = sub i 1 34 put d i uchar add rem v 10 48 35 v = div v 10 36 if eq v 0 37 break 38 end 39 end 40 emit part d i sub 20 i 41 end 42 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 43 fn emiti n long to void 44 if lt n 0 45 emit minus 46 emitu add bitcast ulong not n 1 47 else 48 emitu ulong n 49 end 50 end 51 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 52 note returns how many; one pass over s, so the loop is bounded by len s 53 fn parse s view uchar * to ulong 54 n = ulong 0 55 v = long 0 56 sg = long 1 57 dig = false 58 for i ulong len s 59 c = get s i 60 if eq c 45 61 sg = long -1 62 else 63 if and ge c 48 le c 57 64 v = add mul v 10 long sub c 48 65 dig = true 66 else 67 if dig 68 put vals n mul sg v 69 n = add n 1 70 end 71 v = long 0 72 sg = long 1 73 dig = false 74 end 75 end 76 end 77 if dig 78 put vals n mul sg v 79 n = add n 1 80 end 81 ret n 82 end 83 note item writes the k-th name of the |-separated names text 84 fn item k ulong to void 85 f = ulong 0 86 st = ulong 0 87 for i ulong len names 88 if eq get names i 124 89 if eq f k 90 emit part names st sub i st 91 ret 92 end 93 f = add f 1 94 st = add i 1 95 end 96 end 97 emit part names st sub len names st 98 end 99 note pack runs the 0-1 dynamic program over capacities 0..400: best[c] is the best value with weight <= c 100 note using the items seen so far; keep[i*401+c] records that item i was taken at capacity c 101 fn pack n ulong to void 102 for i ulong n 103 w = ulong get vals mul i 2 104 v = get vals add mul i 2 1 105 for x ulong 401 106 c = sub 400 x 107 if ge c w 108 if gt add get best sub c w v get best c 109 put best c add get best sub c w v 110 put keep add mul i 401 c 1 111 end 112 end 113 end 114 end 115 end 116 fn main to int 117 n = div parse data 2 118 pack n 119 note walk back from capacity 400 over the items in reverse, taking each recorded item 120 c = ulong 400 121 tot = ulong 0 122 for x ulong n 123 i = sub sub n x 1 124 if eq get keep add mul i 401 c 1 125 put vals add mul i 2 1 -1 126 w = ulong get vals mul i 2 127 c = sub c w 128 tot = add tot w 129 end 130 end 131 for i ulong n 132 if eq get vals add mul i 2 1 -1 133 item i 134 emit nl 135 end 136 end 137 emit tw 138 emitu tot 139 emit nl 140 emit tv 141 emiti get best 400 142 emit nl 143 ret 0 144 end 145 146 {{out}} 147

148 map

149 compass

150 water

151 sandwich

152 glucose

153 banana

154 suntan cream

155 waterproof trousers

156 waterproof overclothes

157 note-case

158 sunglasses

159 socks

160 total weight 396

161 total value 1030

162