blib/lib/Games/Puzzles/SendMoreMoney.pm

filter: Statementstmt 85.3% 64 / 75 Branchbran 46.4% 13 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 6 totaltotal 71.2% 89 / 125 CC 16 SCAR 14.5
CC
16
Cov
74%
solve
8.9
tryout
5.6
new
3.0
CRAP 4.3
↑ Summary
1 ###########################################
2 # Games::Puzzles::SendMoreMoney
3 # 2005, Mike Schilli <cpan@perlmeister.com>
4 ###########################################
5  
6 ###########################################
7 package Games::Puzzles::SendMoreMoney;
8 ###########################################
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Algorithm::FastPermute;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = "0.03";
16 1 our $STOP_SOLVER = 0;
17  
18 ###########################################
19 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
20 ###########################################
21 1 my($class, %options) = @_;
22  
23 1 my $self = {
24 puzzle => undef,
25 permutator => undef,
26 search_space => undef,
27 validator => undef,
28 reporter => undef,
29 values => undef,
30 %options,
31 };
32  
33 LOGDIE "Mandatory parameter puzzle missing"
34 1 ▶ unless defined $self->{puzzle};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"puzzle"} 0 1 1
35  
36 1 ($self->{left}, $self->{right}) = split /\s*=\s*/, $self->{puzzle};
37  
38 LOGDIE "Puzzle not an equation: '$self->{puzzle}'"
39 1 ▶ unless defined $self->{right};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"right"} 0 1 1
40  
41 1 bless $self, $class;
42  
43 1 $self->{chars} = [$self->chars_extract($self->{puzzle})];
44
45 1 return $self;
46 }
47  
48 ###########################################
49 1 ▶sub tryout {
Subroutine tryout: called CC=5 SCAR=17.3
Pod undocumented
50 ###########################################
51 1 my($self, $try) = @_;
52  
53 1 ▶ unless($self->{seen}->{join "", values %$try}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"seen"}{join "", values %$try}++) 1 0 1
54 1 ▶ if($self->is_valid($try)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid($try)) 1 0 1
55 1 ▶ if($self->evaluate($try)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->evaluate($try)) 1 0 1
56 1 ▶ $self->{reporter}->($try) if defined $self->{reporter};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"reporter"} 1 0 1
57 1 push @{$self->{results}}, $try;
58 1 return 1;
59 }
60 }
61 }
62  
63 0 return 0;
64 }
65  
66 ###########################################
67 1 ▶sub solve {
Subroutine solve: called CC=6 SCAR=21.9
Pod undocumented
68 ###########################################
69 1 my($self) = @_;
70  
71 1 my %try;
72 1 $self->{results} = ();
73  
74 1 $self->{seen} = {};
75 1 $self->{results} = [];
76  
77 1 ▶ if($self->{permutator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'permutator'}) { } 0 1 1
78 0 ▶ while(my $perm = $self->{permutator}->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $perm = $self->{"permutator"}->next) 0 0 0
79 0 DEBUG "Permutation: @$perm";
80 0 @try{@{$self->{chars}}} = @$perm;
81 0 $self->tryout(\%try);
82 0 ▶ last if $STOP_SOLVER;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Puzzles::SendMoreMoney::STOP_SOLVER 0 0 0
83 }
84 } else {
85 1 my @array = @{$self->{values}};
86 1 eval {
87 1 ▶ permute {
Subroutine __ANON__: called CC=2 SCAR=7.0
88 1 DEBUG "Permutation: @array";
89 1 @try{@{$self->{chars}}} = @array;
90 1 $self->tryout(\%try);
91 1 ▶ die "STOP_SOLVER_SET" if $STOP_SOLVER;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Puzzles::SendMoreMoney::STOP_SOLVER 1 0 1
92 1 } @array;
93 };
94 1 ▶ if($@ and $@ !~ /STOP_SOLVER_SET/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /STOP_SOLVER_SET/) 0 1 1
Condition33%1/3
$@ and not $@ =~ /STOP_SOLVER_SET/
!ll&&!rl&&r
010
MC/DC0%0/2
$@ and not $@ =~ /STOP_SOLVER_SET/
$@ not $@ =~ /STOP_SOLVER_SET/
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /STOP_SOLVER_SET/
ABresult
0X0
100
111
95 0 die "Error is '$@'";
96 }
97 }
98  
99 1 return $self->{results};
100 }
101  
102 ###########################################
103 1 ▶sub is_valid {
Subroutine is_valid: called CC=2 SCAR=7.6
Pod undocumented
104 ###########################################
105 1 my($self, $try) = @_;
106  
107 1 ▶ if(defined $self->{validator}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"validator"}) 0 1 1
108 0 return $self->{validator}->($try);
109 }
110  
111 1 return 1;
112 }
113  
114 ###########################################
115 1 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=7.2
Pod undocumented
116 ###########################################
117 1 my($self, $try) = @_;
118  
119 1 (my $left_tmp = $self->{left}) =~ s/([a-zA-Z])/$try->{$1}/g;
120 1 (my $right_tmp = $self->{right}) =~ s/([a-zA-Z])/$try->{$1}/g;
121
122 # Remove pseudo-octal values
123 1 $left_tmp =~ s/\b0+([1-9])/$1/g;
124 1 $right_tmp =~ s/\b0+([1-9])/$1/g;
125  
126 1 ▶ if(eval $left_tmp == eval $right_tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval $left_tmp == eval $right_tmp) 1 0 1
127 1 INFO "$left_tmp == $right_tmp";
128 1 return 1;
129 }
130  
131 0 DEBUG "$left_tmp != $right_tmp";
132 0 return 0;
133 }
134  
135 ###########################################
136 1 ▶sub chars_extract {
Subroutine chars_extract: called CC=2 SCAR=6.9
Pod undocumented
137 ###########################################
138 1 my($self, $string) = @_;
139  
140 1 my %chars = ();
141  
142 1 ▶ while($string =~ /([a-zA-Z])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /([a-zA-Z])/g) 2 1 3
143 2 $chars{$1}++;
144 }
145  
146 1 return keys %chars;
147 }
148  
149 1 1;
150  
151 __END__
↑ Summary