#!/usr/bin/perl -w use strict; my @table; for () { last if ($_ =~ m/^###/); chomp; push @table, [split / /]; } my %highest = (a => [0,0], b => [0,0], c => [0,0], d => [0,0], sum => 0); for (my $row=0; $row < $#table; $row++) { for (my $col=0; $col <= $#{$table[0]}; $col++ ) { get_highest($row, $col); } } print "The four coherent numbers, which have the highest product:\n"; for my $key (sort keys %highest) { next if ($key eq 'sum'); print "$key => [$highest{$key}->[0],$highest{$key}->[1]] = $table[$highest{$key}->[0]]->[$highest{$key}->[1]]\n"; } sub get_highest { my ($row, $col) = @_; my @steps = qw/c- c-r- r- c+r+ c+ c+r- r+ c-r+/; while (my $step = shift @steps) { my ($tmp_row, $tmp_col) = ($row, $col); my $sum = 0; my @cur_coords; print "Check [$row, $col]\n"; for (0..3) { last unless $table[$tmp_row]->[$tmp_col]; print "S $step\t| R $tmp_row | C $tmp_col\n"; $sum += $table[$tmp_row]->[$tmp_col]; push @cur_coords, [$tmp_row, $tmp_col]; $tmp_row-- if($step =~ m/r\-/); $tmp_col-- if($step =~ m/c\-/); $tmp_row++ if($step =~ m/r\+/); $tmp_col++ if($step =~ m/c\+/); } if ( ($sum > $highest{'sum'}) && (scalar @cur_coords > 3) ) { $highest{'sum'} = $sum; my $i=0; for my $key (sort keys %highest) { next if ($key eq 'sum'); $highest{$key} = [$cur_coords[$i]->[0], $cur_coords[$i]->[1]]; $i++; } } } } #data siehe oben __DATA__