| 1 | package Games::LogicPuzzle; | ||||||||||||||||||||
| 2 | # Perl module to help solve some logic riddles | ||||||||||||||||||||
| 3 | # (C) 2004 Andy Adler | ||||||||||||||||||||
| 4 | # $Id: LogicPuzzle.pm,v 1.6 2006/01/30 20:47:09 adler Exp $ | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | our $VERSION= 0.20; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 13 | 2 | my $class = shift; | |||||||||||||||||||
| 14 | 2 | my %args = @_; | |||||||||||||||||||
| 15 | 2 | my $self = { | |||||||||||||||||||
| 16 | type => 'SimplePuzzle', | ||||||||||||||||||||
| 17 | sameok => 0, | ||||||||||||||||||||
| 18 | verbose => 0, | ||||||||||||||||||||
| 19 | all_solutions => 0, | ||||||||||||||||||||
| 20 | initialized => 0, | ||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||
| 22 | 2 | bless $self, $class; | |||||||||||||||||||
| 23 | 2 | for ( keys %args ) { | |||||||||||||||||||
| 24 | 3 | $self->ProcessArg( $_, $args{$_} ); | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | 2 | return $self; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # the "Computer Science" solution is to do recursion, | ||||||||||||||||||||
| 30 | # but that involves so many method calls, it's sick. | ||||||||||||||||||||
| 31 | # Instead, we autogenerate code to iterate though | ||||||||||||||||||||
| 32 | # problem space | ||||||||||||||||||||
| 33 | 2 | ▶ | sub make_solve_code { | ||||||||||||||||||
|
Subroutine
make_solve_code: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 34 | 2 | my $self= shift(); | |||||||||||||||||||
| 35 | 2 | my $code= ""; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # autogenerate subroutines from properties | ||||||||||||||||||||
| 38 | # such that smoke(beverage=>'beer') = get('smoke',beverage=>'beer') | ||||||||||||||||||||
| 39 | 2 | my %proplist= ( %{$self->{properties}}, | |||||||||||||||||||
| 40 | 2 | %{$self->{assignlist}} ); | |||||||||||||||||||
| 41 | 2 | for my $poss (keys %proplist) { | |||||||||||||||||||
| 42 | 8 | $code.= qq( | |||||||||||||||||||
| 43 | sub $poss { return \$_[0]->get('$poss',\$_[1],\$_[2]) }; | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 2 | my @thing = @{$self->{things}}; | |||||||||||||||||||
| 48 | 2 | my @solve_order; | |||||||||||||||||||
| 49 | 2 | ▶ | if ( $self->{solve_order} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 1 | @solve_order = @{$self->{solve_order}}; | |||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||
| 52 | 1 | @solve_order = keys %{$self->{properties}}; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # setup code, disable warnings for verify | ||||||||||||||||||||
| 56 | 2 | $code.= q( | |||||||||||||||||||
| 57 | my @thing = @{$self->{things}}; | ||||||||||||||||||||
| 58 | my %possesion = %{$self->{properties}}; | ||||||||||||||||||||
| 59 | local $^W; | ||||||||||||||||||||
| 60 | LOOP:); | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # loop through all the possibilities | ||||||||||||||||||||
| 63 | # thus we loop through each property of each thing | ||||||||||||||||||||
| 64 | # ie $thing[ num_things ]->{ property_list } | ||||||||||||||||||||
| 65 | # unless that $thing already has a property | ||||||||||||||||||||
| 66 | 2 | my $brace_count = 0; | |||||||||||||||||||
| 67 | 2 | for my $p ( @solve_order ) { | |||||||||||||||||||
| 68 | 6 | for my $t ( 0 .. $#thing ) { | |||||||||||||||||||
| 69 | 41 | ▶ | if( not defined $self->{things}->[ $t ] ->{ $p } ){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 28 | $code .= sprintf q( | |||||||||||||||||||
| 71 | for (@{$possesion{ "%s" }}) { | ||||||||||||||||||||
| 72 | local $thing[ %d ]->{"%s"} = $_; | ||||||||||||||||||||
| 73 | next unless $self->verify(); ), | ||||||||||||||||||||
| 74 | $p , $t, $p, $t, $p; | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 28 | $brace_count++; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | # now we have a solution, return it if required; | ||||||||||||||||||||
| 82 | 2 | $code.= q( | |||||||||||||||||||
| 83 | push @solutions, clone( \@thing ); | ||||||||||||||||||||
| 84 | last LOOP unless $self->{all_solutions};); | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # add all the close braces for the code | ||||||||||||||||||||
| 87 | 2 | $code.= " }" x $brace_count; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 2 | return $code; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 2 | ▶ | sub solve { | ||||||||||||||||||
|
Subroutine
solve: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||
| 93 | 2 | my $self = shift; | |||||||||||||||||||
| 94 | 2 | ▶ | $self->initialize() unless $self->{initialized}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 2 | my $code = $self->make_solve_code(); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | my @solutions; | |||||||||||||||||||
| 98 | 0 | ▶ | eval $code; croak $@ if $@; | ||||||||||||||||||
|
Subroutine
beverage: called CC=1 SCAR=0.2
Subroutine
housecolour: called CC=1 SCAR=0.2
Subroutine
houseposition: called CC=1 SCAR=0.2
Subroutine
nationality: not called CC=1 SCAR=0.2
Subroutine
pet: called CC=1 SCAR=0.2
Subroutine
smokebrand: called CC=1 SCAR=0.2
Subroutine
POS: not called CC=1 SCAR=0.2
Subroutine
VAL: called CC=1 SCAR=0.2
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 2 | ▶ | return undef unless @solutions; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 2 | ▶ | return $solutions[0] unless $self->{all_solutions}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 0 | return \@solutions; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 3578 | ▶ | sub verify { | ||||||||||||||||||
|
Subroutine
verify: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 106 | 3578 | my $self= shift; | |||||||||||||||||||
| 107 | 3578 | ▶ | if (! $self->{sameok}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 3570 | ▶ | return 0 unless $self->verify_not_same(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 1690 | my $verify_proc= $self->{verify_proc}; | |||||||||||||||||||
| 111 | 1690 | ▶ | return 0 unless $verify_proc->( $self, $self->{things} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 727 | return 1; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | # check that no two things have same members | ||||||||||||||||||||
| 117 | 3570 | ▶ | sub verify_not_same { | ||||||||||||||||||
|
Subroutine
verify_not_same: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 118 | 3570 | my $self= shift; | |||||||||||||||||||
| 119 | 3570 | my @things= @{$self->{things}}; | |||||||||||||||||||
| 120 | 3570 | my @posses= keys %{$self->{properties}}; | |||||||||||||||||||
| 121 | 3570 | for my $cat (@posses) { | |||||||||||||||||||
| 122 | 16018 | my %verif; | |||||||||||||||||||
| 123 | 16018 | for my $thing (@things) { | |||||||||||||||||||
| 124 | 78374 | my $thing_cat = $thing->{$cat}; | |||||||||||||||||||
| 125 | 78374 | ▶ | next unless $thing_cat; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 22629 | ▶ | return 0 if $verif{$thing_cat}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 20741 | $verif{$thing_cat}=1; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | 1682 | return 1; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 2 | ▶ | sub initialize { | ||||||||||||||||||
|
Subroutine
initialize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 135 | 2 | my $self = shift; | |||||||||||||||||||
| 136 | 2 | my @things= (); | |||||||||||||||||||
| 137 | 2 | my @posses= keys %{$self->{properties}}; | |||||||||||||||||||
| 138 | 2 | my @assign= keys %{$self->{assignlist}}; | |||||||||||||||||||
| 139 | 2 | for my $n (1 .. $self->{num_things}) { | |||||||||||||||||||
| 140 | 21 | my %thing; | |||||||||||||||||||
| 141 | 21 | for (@posses) { | |||||||||||||||||||
| 142 | 41 | $thing{$_}= undef | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | 21 | for (@assign) { | |||||||||||||||||||
| 145 | 37 | $thing{$_}= $self->{assignlist}->{$_}->[$n-1]; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | 21 | push @things, \%thing; | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | 2 | $self->{things} = \@things; | |||||||||||||||||||
| 150 | 2 | $self->{initialized}= 1; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | # get all things (pers) who have $cat eq $val | ||||||||||||||||||||
| 154 | 21511 | ▶ | sub getpers { | ||||||||||||||||||
|
Subroutine
getpers: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 155 | 21511 | my ($things, $cat, $val) = @_; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 21511 | ▶ | return undef unless defined $val; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 21511 | my @getpers= grep { $_->{$cat} and | |||||||||||||||||||
| 159 | 109788 | ▶ | $_->{$cat} eq $val } @$things; | ||||||||||||||||||
|
Condition100%3/3
$_->{$cat} && $_->{$cat} eq $val
MC/DC100%2/2
$_->{$cat} && $_->{$cat} eq $val
$_->{$cat}
$_->{$cat} eq $val
Truth table100%3/3
A && B
A= $_->{$cat}B= $_->{$cat} eq $val
| |||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 21511 | return @getpers; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # get the $want possession of the thing who's $cat is $val | ||||||||||||||||||||
| 165 | 21511 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 166 | 21511 | my ($self, $want, $cat, $val, $soln) = @_; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 21511 | my $things= $self->{things}; | |||||||||||||||||||
| 169 | 21511 | ▶ | $things= $soln if $soln; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 21511 | my @getpers= getpers($things, $cat, $val ); | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 21511 | ▶ | return undef unless @getpers; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 11734 | my @getwant= map {$_->{$want}} @getpers; | |||||||||||||||||||
| 174 | 11734 | return $getwant[0]; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 2 | my %cmds = ( | |||||||||||||||||||
| 179 | num_things => \&num_things, | ||||||||||||||||||||
| 180 | properties => \&properties, | ||||||||||||||||||||
| 181 | sameok => \&sameok, | ||||||||||||||||||||
| 182 | verify_proc => \&verify_proc, | ||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 3 | ▶ | sub ProcessArg { | ||||||||||||||||||
|
Subroutine
ProcessArg: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 186 | 3 | my $self = shift; | |||||||||||||||||||
| 187 | 3 | my ($cmd, $detail) = @_; | |||||||||||||||||||
| 188 | 3 | ▶ | if ($cmds{$cmd}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | 3 | $cmds{$cmd}->($self, $detail ); | |||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||
| 191 | 0 | die "Can't $cmd $detail"; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 2 | ▶ | sub num_things { | ||||||||||||||||||
|
Subroutine
num_things: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 196 | 2 | my $self = shift; | |||||||||||||||||||
| 197 | 2 | $self->{num_things}= shift(); | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | # properties are properties to be distributed to things | ||||||||||||||||||||
| 201 | 2 | ▶ | sub properties { | ||||||||||||||||||
|
Subroutine
properties: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 202 | 2 | my $self = shift; | |||||||||||||||||||
| 203 | 2 | $self->{properties}= shift(); | |||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | # assign are properties that are preassigned to things | ||||||||||||||||||||
| 207 | 2 | ▶ | sub assign { | ||||||||||||||||||
|
Subroutine
assign: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 208 | 2 | my $self = shift; | |||||||||||||||||||
| 209 | 2 | $self->{assignlist}= shift(); | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 1 | ▶ | sub sameok { | ||||||||||||||||||
|
Subroutine
sameok: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 213 | 1 | my $self = shift; | |||||||||||||||||||
| 214 | 1 | $self->{sameok}= shift(); | |||||||||||||||||||
| 215 | } | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | 2 | ▶ | sub verify_proc { | ||||||||||||||||||
|
Subroutine
verify_proc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 218 | 2 | my $self = shift; | |||||||||||||||||||
| 219 | 2 | $self->{verify_proc}= shift(); | |||||||||||||||||||
| 220 | } | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | # cheap clone routine | ||||||||||||||||||||
| 223 | 2 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 224 | 2 | my @data= @{shift()}; | |||||||||||||||||||
| 225 | 2 | my @copy; | |||||||||||||||||||
| 226 | 2 | for (@data) { | |||||||||||||||||||
| 227 | 21 | my %data= ( %{$_} ); | |||||||||||||||||||
| 228 | 21 | push @copy, \%data; | |||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | 2 | return \@copy; | |||||||||||||||||||
| 231 | } | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 1 | ▶ | sub solve_order { | ||||||||||||||||||
|
Subroutine
solve_order: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 234 | 1 | my $self = shift; | |||||||||||||||||||
| 235 | 1 | $self->{solve_order}= shift(); | |||||||||||||||||||
| 236 | } | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | 2 | 1; | |||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||