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