#!/usr/bin/perl -Tw
#
# http://projecteuler.net/
#
# Problem 8
# Find the greatest product of five consecutive digits in the 1000-digit number.
# (Number is stored in p8.input.)
#
# Solution by Melkor (Filip Niksic, fniksic@gmail.com)
#
###

use warnings;
use strict;

sub readfile {
    undef $/;

    open my $file, shift;
    my $line = <$file>;
    $line =~ s/\s//g;

    our @broj = split '', $line;
}

sub solve {
    our @broj;
    my ($m, $k, $max) = (1, 0, 0);

    for (my $i = 0; $i < @broj; ++$i) {
	$m *= $broj[$i];
	if ($m == 0) {
	    $m = 1;
	    $k = 0;
	}
	else {
	    if ($k == 5) {$m /= $broj[$i-5]}
	    else {$k++}
	    if ($k == 5 && $m > $max) {$max = $m}
	}
    }

    print "Result: $max\n";
}

readfile @ARGV;
solve;

