067 Maximum path sum II.pl 637 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # Find the maximum total from top to bottom in triangle.txt, a 15K text file containing a triangle with one-hundred rows.
  6. # https://projecteuler.net/problem=67
  7. use 5.010;
  8. use List::Util qw(max);
  9. # usage: perl problem_067.pl < p067_triangle.txt
  10. my @triangle;
  11. while (<>) {
  12. push @triangle, [split(' ')];
  13. }
  14. foreach my $i (reverse(0 .. $#triangle - 1)) {
  15. foreach my $j (0 .. $#{$triangle[$i]}) {
  16. $triangle[$i][$j] = $triangle[$i][$j] + max($triangle[$i+1][$j], $triangle[$i+1][$j+1]);
  17. }
  18. }
  19. say $triangle[0][0];