# PWC 100 use strict; use warnings; use JSON::PP; use Test::More; use List::Util qw/reduce/; while ( my $line = ) { my ($json, $expected) = split /\s/, $line; my $aoa = decode_json($json); is( compute($aoa), $expected ); compute($aoa); } sub compute { my @aoa = @{(shift)}; my $bottom; while (@aoa) { $bottom = pop @aoa; my $i=0; reduce { # by overlapping pairs $aoa[-1][$i++] += # add to parent ... ($a < $b ? $a : $b); # ... minimum neighbors $b # right to left nb } @$bottom; } return $bottom->[0]; } done_testing; __DATA__ [[1],[2,4],[6,4,9],[5,1,7,2]] 8 [[9],[1,6],[7,8,2],[5,8,2,3]] 19