#! perl -slw use strict; use List::Util qw[ reduce ]; use Data::Dump qw[ pp ]; sub canBeMadeFrom { my( $sigRef, $word ) = @_; for( split '', $word ) { return unless exists $sigRef->{ $_ }; delete $sigRef->{ $_ }; } return 1; } my %stats; while( <> ) { chomp; next if length() > 8; my $sig = join '', sort split '', $_; push @{ $stats{ $sig } }, $_; } print STDERR scalar keys %stats; my @eights = grep length() == 8, keys %stats; my %eights; @eights{ @eights } = delete @stats{ @eights }; print my $i = @eights; for my $eight ( keys %eights ) { printf STDERR "\r%5d '%s'\t", $i--, $eight; my %sig; undef @sig{ split '', $eight }; for my $lesser ( keys %stats ) { next unless canBeMadeFrom( { %sig }, $lesser ); for my $word ( @{ $stats{ $lesser } } ) { push @{ $eights{ $eight } }, $word if canBeMadeFrom( { %sig }, $word ); } } } my @ordered = sort{ @{ $eights{ $b } } <=> @{ $eights{ $a } } } keys %eights; for( 0 .. $#ordered ) { print "$ordered[ $_ ]: ", scalar @{ $eights{ $ordered[ $_ ] } }; print "@{ $eights{ $ordered[ $_ ] } }"; }