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