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/maximum-triangle-path-sum/maximum-triangle-path-sum.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 / maximum-triangle-path-sum / maximum-triangle-path-sum.om` 111 lines ยท 2790 bytes Raw Blame History 1 note Rosetta Code task "Maximum triangle path sum": https://rosettacode.org/wiki/Maximum_triangle_path_sum 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 tri "55 94 48 95 30 96 77 71 26 67 97 13 76 38 45 07 36 79 16 37 68 48 07 09 18 70 26 06 18 72 79 46 59 79 29 90 20 76 87 11 32 07 07 49 18 27 83 58 35 71 11 25 57 29 85 14 64 36 96 27 11 58 56 92 18 55 02 90 03 60 48 49 41 46 33 36 47 23 92 50 48 02 36 59 42 79 72 20 82 77 42 56 78 38 80 39 75 02 71 66 66 01 03 55 72 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36 85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52 06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15 27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93" 8 text tsmall "55 94 48 95 30 96 77 71 26 67" 9 text tmax "maximum path sum: " 10 global vals long 171 11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 12 fn emit a view uchar * to void 13 o = ulong 0 14 while lt o len a bound 4096 15 k = write 1 a o sub len a o 16 if le k 0 17 exit 1 18 end 19 o = add o ulong k 20 end 21 end 22 note emitu writes n in decimal; 20 digits hold every ulong 23 fn emitu n ulong to void 24 d = new uchar 20 0 25 i = ulong 20 26 v = n 27 while true bound 20 28 i = sub i 1 29 put d i uchar add rem v 10 48 30 v = div v 10 31 if eq v 0 32 break 33 end 34 end 35 emit part d i sub 20 i 36 end 37 note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow 38 fn emiti n long to void 39 if lt n 0 40 emit minus 41 emitu add bitcast ulong not n 1 42 else 43 emitu ulong n 44 end 45 end 46 note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and 47 note returns how many; one pass over s, so the loop is bounded by len s 48 fn parse s view uchar * to ulong 49 n = ulong 0 50 v = long 0 51 sg = long 1 52 dig = false 53 for i ulong len s 54 c = get s i 55 if eq c 45 56 sg = long -1 57 else 58 if and ge c 48 le c 57 59 v = add mul v 10 long sub c 48 60 dig = true 61 else 62 if dig 63 put vals n mul sg v 64 n = add n 1 65 end 66 v = long 0 67 sg = long 1 68 dig = false 69 end 70 end 71 end 72 if dig 73 put vals n mul sg v 74 n = add n 1 75 end 76 ret n 77 end 78 note fold adds to vals[p] the larger of vals[q] and vals[q+1], the two entries below it 79 fn fold p ulong q ulong to void 80 lft = get vals q 81 rgt = get vals add q 1 82 if gt lft rgt 83 put vals p add get vals p lft 84 else 85 put vals p add get vals p rgt 86 end 87 end 88 note rows are stored one after another, so row r starts at r*(r+1)/2; the sum folds from the bottom row 89 note up, each entry adding the larger of the two entries below it, until vals[0] holds the answer 90 fn best rows ulong to long 91 for x ulong sub rows 1 92 r = sub sub rows x 2 93 b = div mul r add r 1 2 94 nb = add b add r 1 95 for c ulong add r 1 96 fold add b c add nb c 97 end 98 end 99 ret get vals 0 100 end 101 fn main to int 102 n = parse tsmall 103 emit tmax 104 emiti best 4 105 emit nl 106 n = parse tri 107 emit tmax 108 emiti best 18 109 emit nl 110 ret 0 111 end