#!/usr/bin/env perl use 5.010; use strict; use warnings; use autodie; die "Usage: $0 dictionary solution max_letters\n" unless @ARGV == 3; my ($DICT, $SOLUTION, $MAX) = @ARGV; my $count = 0; open my $fh, '<', $DICT; while (<$fh>) { s/%?\r?\n?$//; next if length > $MAX; if (matching_word($_)) { ++$count; say; } } close $fh; say "Total: $count"; sub matching_word { state $solution_letters = [ split '' => $SOLUTION ]; my ($word) = @_; foreach my $letter (@$solution_letters) { my $pos = index $word, $letter; if ($pos != -1) { substr($word, $pos, 1) = ''; return 1 if length $word == 0; } } return 0; }