#!perl -w

# Find words which become the same if all occurrences of one letter
# are change to a different letter
#
# Copyright 2017 Ken Takusagawa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


#open FI,'/usr/share/dict/american-english-huge' or die;
while(<STDIN>){
    chomp;
    die if /_/;  #our placeholder
    die if /\^/;  #our placeholder for uppercase letters
    push@words,$_;
}
#flush so tee can see progress
#$|=1;
#no more need to flush because huge output
for$coll('aa'..'zz'){
    die unless ($x,$y)=($coll=~/(.)(.)/);
    next unless $x lt $y;
    $Xx=uc$x;
    $Yy=uc$y;
    #print$coll;
    undef %c;
    for$orig(@words){
	#because $_ is a reference and modifies the array
	$_=$orig;
	s/$x/_/g;
	s/$y/_/g;
	s/$Xx/\^/g;
	s/$Yy/\^/g;
        push @{$c{$_}},$orig;
    }
    $bad=0;
    for(keys %c){
	$n=scalar@{$c{$_}};
	#penalty function = number of diagonals of N points.
	$bad+=$n*($n-1)/2;
	#$bad+=$c{$_}-1;
	#$bad++ if $c{$_}>1;
    }
    print "score $bad $coll\n";
    for(sort keys%c){
        $n=scalar@{$c{$_}};
        next unless $n>1;
        print "item $_ ";
        print scalar@{$c{$_}};
        for(@{$c{$_}}){
            print " $_";
        }
        print "\n";
    }
}

# 14 minutes huge with full list
