Thread Project Euler (56 answers)
Opened by Ronnie at 2007-12-15 12:19

styx-cc
 2007-12-30 22:08
#104282 #104282
User since
2006-05-20
533 Artikel
BenutzerIn

user image
So , hab das nochmal getestet und die while-Schleife (hoffentlich richtig) eingebaut, laut den Testausgaben laeuft das Script richtig.
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl -w
use strict;

my @table;
for (<DATA>) {
  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__

Ausgabe:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Check [19, 18]
S c- | R 19 | C 18
S c- | R 19 | C 17
S c- | R 19 | C 16
S c- | R 19 | C 15
Check [19, 18]
S c-r- | R 19 | C 18
S c-r- | R 18 | C 17
S c-r- | R 17 | C 16
S c-r- | R 16 | C 15
Check [19, 18]
S r- | R 19 | C 18
S r- | R 18 | C 18
S r- | R 17 | C 18
S r- | R 16 | C 18
Check [19, 18]
S c+r+ | R 19 | C 18
Check [19, 18]
S c+ | R 19 | C 18
S c+ | R 19 | C 19
Check [19, 18]
S c+r- | R 19 | C 18
S c+r- | R 18 | C 19
Check [19, 18]
S r+ | R 19 | C 18
Check [19, 18]
S c-r+ | R 19 | C 18
Check [19, 19]
S c- | R 19 | C 19
S c- | R 19 | C 18
S c- | R 19 | C 17
S c- | R 19 | C 16
Check [19, 19]
S c-r- | R 19 | C 19
S c-r- | R 18 | C 18
S c-r- | R 17 | C 17
S c-r- | R 16 | C 16
Check [19, 19]
S r- | R 19 | C 19
S r- | R 18 | C 19
S r- | R 17 | C 19
S r- | R 16 | C 19
Check [19, 19]
S c+r+ | R 19 | C 19
Check [19, 19]
S c+ | R 19 | C 19
Check [19, 19]
S c+r- | R 19 | C 19
Check [19, 19]
S r+ | R 19 | C 19
Check [19, 19]
S c-r+ | R 19 | C 19
The four coherent numbers, which have the highest product:
a => [12,6] = 89
b => [13,5] = 94
c => [14,4] = 97
d => [15,3] = 87


Hab ich irgendwo n Denkfehler?
Pörl.

View full thread Project Euler