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