#!/usr/bin/perl use strict; use warnings; use Mystery::Word; my $puzzle = Mystery::Word->new( size => 5 ); $puzzle->hint( bumps => 2, seams => 2, domes => 3, shake => 3, pokes => 3, dukes => 3 ); my @solutions = $puzzle->solve(); print "$_\n" for @solutions; __END__ ################################## package Mystery::Word; use strict; use warnings; use Carp; use constant WORD => 0; use constant SIZE => 1; use constant COMMON => 2; use constant COMBOS => 3; our $VERSION = '0.01'; sub new { my $class = shift; croak "Incorrect number of parameters for 'new'" if @_ % 2; my $self = bless {}, $class; $self->_init( @_ ); return $self; } sub _init { my ($self, %opt) = @_; $opt{size} = force_numeric( $opt{size} ); $opt{size} ||= 0; croak "'size' parameter must be set to a positive whole integer" if ! $opt{size}; $self->{target} = $opt{size}; } sub hint { my $self = shift; croak "Incorrect number of parameters for 'hint'" if @_ % 2; my %hint = @_; for my $word ( keys %hint ) { carp "$word contains non alpha characters" if $word =~ tr/a-z//c; push @{ $self->{hint} }, [ lc $word, length $word, force_numeric( $hint{ $word } ), combinations(length $word, $hint{ $word }) ]; } } sub combinations { my ($n, $k) = @_; return factorial( $n ) / ( factorial( $k ) * factorial( $n - $k ) ); } sub factorial { my $n = shift; my $factorial = 1; $factorial *= $_ for 1 .. $n; return $factorial; } sub force_numeric { my $num = shift; return 0 if ! defined $num || ! length $num; no warnings 'numeric'; return abs( int( $num ) ); } sub solve { my $self = shift; @{ $self->{hint} } = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $b->[2] <=> $a->[2] } map { [ $_, $_->[COMBOS], $_->[COMMON] ] } @{ $self->{hint} }; # Here is where I got stuck } "This statement is false";