Thread Rätsel: mit Perl lösbar? (22 answers)
Opened by lichtkind at 2005-01-13 16:50

esskar
 2005-01-14 06:05
#19413 #19413
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
klar geht das:
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
58
59
60
61
62
63
64
#!/usr/bin/perl

use strict;
use warnings;

my @Eq = (
" ABC + DDE =  FCF",
"  CB + BGE =  BCA",
" AEE + CBG = DGAE",
" ABC +  CB =  AEE",
   " DDE + BGE =  CBG",
   " FCF + BCA = DGAE",
);

my @res = &Sol(@Eq);

foreach my $r (@res)
{
print join("; ", map { "$_ = $r->{$_}" } keys %{$r}), "\n";
}

sub Sol
{
my (@e) = @_;

my %pairs = ();
my @sol = ();

for (@e)
{
s!=!==!;
$pairs{$_} = 0 foreach (m!(\w)!g);
}

my @vars = keys %pairs;
my $count = scalar @vars;

while($pairs{$vars[$count-1]} < 10)
{
my $true = 0;
foreach my $e (@e)
{
while(my($k, $v) = each %pairs)
{
$e =~ s!$k!$v!g;
}

$true = eval $e;
last unless $true;
}

push @sol, {%pairs} if $true;

my $i = 0;
while($i < $count)
{
last if ++$pairs{$vars[$i]} < 10;
$pairs{$vars[$i]} = 0;
$i++;
}
}

return @sol;
}


wohl diese brute-force methode ziemlich ungeignet ist.
Komplexität O(n^10) mit n = Anzahl der Variablen

View full thread Rätsel: mit Perl lösbar?