# PWC 100 use strict; use warnings; use feature 'say'; use JSON::PP; use Test::More; while ( my $line = ) { my ($json, $expected) = split /\s/, $line; my $aoa = decode_json($json); is( compute($aoa), $expected ); } sub compute { my @aoa = @{shift()}; my $total; for my $r1_ix (0,1) { # second row for my $r2_ix ($r1_ix, $r1_ix+1) { # third row for my $r3_ix ($r2_ix, $r2_ix+1) { # fourth row my $sum = $aoa[0][0] + $aoa[1][$r1_ix] + $aoa[2][$r2_ix] + $aoa[3][$r3_ix]; say sprintf('Sum of %d (%d, %d, %d, %d)', $sum, $aoa[0][0], $aoa[1][$r1_ix], $aoa[2][$r2_ix], $aoa[3][$r3_ix]); $total = $sum if ! $total || $total > $sum; } } } return $total; } 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