Games::Puzzles::SendMoreMoney

Solve SEND+MORE=MONEY problems
Download

Games::Puzzles::SendMoreMoney Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Michael Schilli
  • Publisher web site:
  • http://search.cpan.org/~mschilli/

Games::Puzzles::SendMoreMoney Tags


Games::Puzzles::SendMoreMoney Description

Solve SEND+MORE=MONEY problems Games::Puzzles::SendMoreMoney is a Perl module that solves numerical puzzles like to following: Assume that each of the letters in the following expression represents a distinct numerical digit: SEND + MORE = MONEYGames::Puzzles::SendMoreMoney will crack this puzzle by brute-forcing the entire search space. In its simplest form, a call to its constructor specifies the puzzle and a range of digit values for each letter in the puzzle: # Either ... my $solver = Games::Puzzles::SendMoreMoney->new( values => , puzzle => "SEND + MORE = MONEY", };Calling the solver will then run through all possible permutations and return a reference to an array of results: my $result = $solver->solve();A single result (hence, an element of the array which $result is pointing to) consists of a reference to a hash containing the mapping between the puzzle letters and their values: $VAR1 = { 'S' => 9, 'O' => 0, 'M' => 1, 'D' => 7, 'N' => 6, 'R' => 8, 'E' => 5, 'Y' => 2 };Often times, however, going through the entire search space can be extremely time consuming. Instead, it is desirable to report a result as soon as it is found: my $solver = Games::Puzzles::SendMoreMoney->new( values => , puzzle => "SEND + MORE = MONEY", reporter => sub { print Dumper($_) }, );The reporter parameter specifies a reference to a function, which will be called by Games::Puzzles::SendMoreMoney on every result that matches the puzzle expression. The reporter function will get a reference to a result hash as its first parameter. On top of that, the reporter can set the variable $Games::Puzzles::SendMoreMoney::STOP_SOLVER to a true value to indicate that the solver should terminate immediately. (However, this doesn't work for the default permutator yet).Sometimes, not all possible permutations are valid. For example, the original form of the SEND+MORE=MONEY puzzle requires that neither of the numbers in the puzzle has a leading zero. These types of constraints can be specified by using a validator function, which will be called before evaluating a combination: my $solver = Games::Puzzles::SendMoreMoney->new( values => , puzzle => "SEND + MORE = MONEY", reporter => sub { print Dumper($_) }, validator => sub { return 0 if $_->{S} == 0; return 0 if $_->{M} == 0; return 1; }, );If the validator returns 0, Games::Puzzles::SendMoreMoney won't even evaluate the permutation but instead move on to the next one immediately.Games::Puzzles::SendMoreMoney also supports custom permutators, which need to return arrays of numbers which will be mapped to the puzzle letters somewhat unpredictably: # ... or ... my $solver = Games::Puzzles::SendMoreMoney->new( permutator => &get_next_permutation, puzzle => "SEND + MORE = MONEY", };At some point, Games::Puzzles::SendMoreMoney will even support a narrowly defined search space (however, currently, this isn't implemented): # ... or ... my $solver = Games::Puzzles::SendMoreMoney->new( search_space => { S => , E => , N => , D => , # ... puzzle => "SEND + MORE = MONEY", };SYNOPSIS use Games::Puzzles::SendMoreMoney; use Data::Dumper; my $solver = Games::Puzzles::SendMoreMoney->new( values => , puzzle => "SEND + MORE = MONEY", reporter => sub { print Dumper($_) }, validator => sub { return 0 if $_->{S} == 0; return 0 if $_->{M} == 0; return 1; }, ); $solver->solve(); Requirements: · Perl


Games::Puzzles::SendMoreMoney Related Software