Jump to content

Maximum triangle path sum: Difference between revisions

Added zkl
m (Added missing "task" template)
(Added zkl)
Line 443:
{{out}}
<pre>
1320
</pre>
 
=={{header|zkl}}==
{{trans|Python}}
The two Python solutions:
<lang zkl>tri:=File("triangle.txt").pump(List,fcn(s){ s.strip().split(" ").apply("toInt") }).copy();
while(tri.len()>1){
t0:=tri.pop();
t1:=tri.pop();
tri.append( [[(it); t1.enumerate();
'wrap([(i,t)]){ t + t0[i].max(t0[i+1]) }]])
}
tri[0][0].println();</lang>
<lang zkl>data:=File("triangle.txt").pump(List,fcn(s){ s.strip().split(" ").apply("toInt") });
fcn f(x,y,z){ x + y.max(z) }
fcn g(xs,ys){ Utils.zipWith(f,ys,xs,xs[1,*]); }
data.reverse().reduce(g)[0].println();</lang>
{{trans|Go}}
<lang zkl>lines:=File("triangle.txt").pump(List,fcn(s){ s.strip().split(" ").apply("toInt") });
d:=lines[-1].copy();
foreach row in ([lines.len()-2..0,-1]){
d1:=d[1,*];
l :=d[0];
foreach i,u in (lines[row].enumerate()){
d[i] = u + l.max(r:=d1[i]);
l = r;
}
}
println(d[0]);</lang>
{{out}}
<pre>
1320
1320
1320
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.