Maximum triangle path sum: Difference between revisions

→‎{{header|Perl 6}}: supply right-associative solution
(→‎{{header|Perl 6}}: more explanation)
(→‎{{header|Perl 6}}: supply right-associative solution)
Line 69:
{{out}}
<pre>1320</pre>
Here's a more FPish version with the same output. We define our own operator and the use it in the reduction metaoperator form, <tt>[op]</tt>, which turns any infix into a list operator. (We could presumably have avoided the <tt>reverse</tt> by defining our op as equivalent to a right-associative operator instead, but the reversal works too, and seems more natural in this case.)
<lang perl6>sub infix:<op>(@a,@b) { (@a Zmax @a.rotate) Z+ @b }
 
say [op] slurp("triangle.txt").lines.reverse.map: { [.words] }</lang>
Instead of using reverse, one could also define the op as right-associative.
<lang perl6>sub infix:<op>(@a,@b) is assoc('right') { @a Z+ (@b Zmax @b.rotate) }
 
say [op] slurp("triangle.txt").lines.map: { [.words] }</lang>
 
=={{header|Python}}==
Anonymous user