Jump to content

Maximum triangle path sum: Difference between revisions

(Not a draft any more)
Line 194:
<pre>%1 = 1320</pre>
 
=={{header|Perl}}==
<lang perl>use 5.10.0;
use List::Util 'max';
 
my @sum;
while (<>) {
my @x = split;
@sum = ($x[0] + $sum[0],
map($x[$_] + max(@sum[$_-1, $_]), 1 .. @x-2),
$x[-1] + $sum[-1]);
}
 
say max(@sum);</lang>
{{out}}
<pre>
% perl maxpath.pl triangle.txt
1320
</pre>
=={{header|Perl 6}}==
The <tt>Z+</tt> and <tt>Zmax</tt> are examples of the zipwith metaoperator. We ought to be able to use <tt>[Z+]=</tt> as an assignment operator here, but rakudo has a bug. Note also we can use the <tt>Zmax</tt> metaoperator form because <tt>max</tt> is define as an infix in Perl 6.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.