interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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