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-mv/blob/main/si/rosetta/tasks/water-collected-between-towers/water-collected-between-towers.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / water-collected-between-towers / water-collected-between-towers.om` 156 lines ยท 2685 bytes Raw Blame History 1 note Rosetta Code task "Water collected between towers": https://rosettacode.org/wiki/Water_collected_between_towers 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 d0 "1 5 3 7 2" 8 text d1 "5 3 7 2 6 4 5 9 1 2" 9 text d2 "2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1" 10 text d3 "5 5 5 5" 11 text d4 "5 6 7 8" 12 text d5 "8 7 7 6" 13 text d6 "6 7 10 7 6" 14 text twater "water: " 15 global vals long 16 16 global lmax long 16 17 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 18 fn emit a view uchar * to void 19 o = ulong 0 20 while lt o len a bound 4096 21 k = write 1 a o sub len a o 22 if le k 0 23 exit 1 24 end 25 o = add o ulong k 26 end 27 end 28 note emitu writes n in decimal; 20 digits hold every ulong 29 fn emitu n ulong to void 30 d = new uchar 20 0 31 i = ulong 20 32 v = n 33 while true bound 20 34 i = sub i 1 35 put d i uchar add rem v 10 48 36 v = div v 10 37 if eq v 0 38 break 39 end 40 end 41 emit part d i sub 20 i 42 end 43 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 44 fn emiti n long to void 45 if lt n 0 46 emit minus 47 emitu add bitcast ulong not n 1 48 else 49 emitu ulong n 50 end 51 end 52 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 53 note returns how many; one pass over s, so the loop is bounded by len s 54 fn parse s view uchar * to ulong 55 n = ulong 0 56 v = long 0 57 sg = long 1 58 dig = false 59 for i ulong len s 60 c = get s i 61 if eq c 45 62 sg = long -1 63 else 64 if and ge c 48 le c 57 65 v = add mul v 10 long sub c 48 66 dig = true 67 else 68 if dig 69 put vals n mul sg v 70 n = add n 1 71 end 72 v = long 0 73 sg = long 1 74 dig = false 75 end 76 end 77 end 78 if dig 79 put vals n mul sg v 80 n = add n 1 81 end 82 ret n 83 end 84 note show writes vals[0..n-1] separated by spaces, then a newline 85 fn show n ulong to void 86 for i ulong n 87 if gt i 0 88 emit sp 89 end 90 emiti get vals i 91 end 92 emit nl 93 end 94 note water: a column holds min(highest to its left, highest to its right) minus its own height; 95 note lmax[i] is the running maximum from the left, the right maximum is kept while walking back 96 fn water n ulong to long 97 m = long 0 98 for i ulong n 99 if gt get vals i m 100 m = get vals i 101 end 102 put lmax i m 103 end 104 r = long 0 105 w = long 0 106 for x ulong n 107 i = sub sub n x 1 108 if gt get vals i r 109 r = get vals i 110 end 111 h = get lmax i 112 if lt r h 113 h = r 114 end 115 w = add w sub h get vals i 116 end 117 ret w 118 end 119 fn main to int 120 n = parse d0 121 show n 122 emit twater 123 emiti water n 124 emit nl 125 n = parse d1 126 show n 127 emit twater 128 emiti water n 129 emit nl 130 n = parse d2 131 show n 132 emit twater 133 emiti water n 134 emit nl 135 n = parse d3 136 show n 137 emit twater 138 emiti water n 139 emit nl 140 n = parse d4 141 show n 142 emit twater 143 emiti water n 144 emit nl 145 n = parse d5 146 show n 147 emit twater 148 emiti water n 149 emit nl 150 n = parse d6 151 show n 152 emit twater 153 emiti water n 154 emit nl 155 ret 0 156 end