Maximum triangle path sum: Difference between revisions
Content added Content deleted
(Not a draft any more) |
|||
Line 194: | Line 194: | ||
<pre>%1 = 1320</pre> |
<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}}== |
=={{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. |
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. |