#!/usr/bin/perl -w # # Very ugly brute force script to find all possible word groupings # for qwantz.com anagram puzzle from 2010 March 1st. # # Prints current progress to STDERR, and each successful match to # a file called 'possibles' # # Hastily scripted 2010 March 2nd by K.T.Kutani # use strict; my $file = shift || "validwords.txt"; my %hash = (); my %store = (); my @sent = (); my @wlist = (); open(FILE,$file); foreach() { chomp($_); (my $n, my $i) = split /\s+/, $_, 2; if( $n =~ /\d+/ ) { #print "$i\n"; push @wlist, $i; } else { push @wlist, $n; } } close(FILE); addletter("t",12); addletter("o",10); addletter("e",8); addletter("a",7); addletter("l",6); addletter("n",6); addletter("u",6); addletter("i",5); addletter("s",5); addletter("d",5); addletter("h",5); addletter("y",5); addletter("I",3); addletter("r",3); addletter("f",3); addletter("w",2); addletter("b",2); #addletter("!",2); addletter("g",1); addletter("k",1); addletter("v",1); addletter("c",1); addletter("m",1); #addletter(":",1); #addletter(",",1); my $v = undef; my $s = undef; my $c = undef; open(FILE,">>possibles"); foreach( @wlist ) { if( ! check() ) { foreach my $neh ( @sent ) { print FILE "$neh "; } print FILE "\n"; } } close(FILE); sub check { my $i = undef; print "Trying: " . printsent(\@sent); foreach( @wlist ) { unless( checkword($_) ) { push @sent, $_; if( checkhash() ) { if( check() ) { returnword( pop @sent ); $i = 1; } else { $i = 0; } } else { $i = 0; } } else { $i = 1; } } return $i; } sub printsent { my $i = shift; my @s = @{$i}; foreach( @s ) { print "$_ "; } print "\n"; } sub addletter { my $let = shift; my $num = shift; $hash{$let} = $num; $store{$let} = 1; } sub checkhash { foreach( keys %hash ) { if( $hash{$_} ) { return 1; } } return 0; } sub checkword { my $word = shift; my @st = (); my @l = split //, $word; foreach( @l ) { if( ! $store{"$_"} ) { if( ! scalar @st ) { return 1; } foreach my $i (@st) { $hash{"$i"}++; } return 1; } if( ! $hash{"$_"} ) { if( ! scalar @st ) { return 1; } foreach my $i (@st) { $hash{"$i"}++; } return 1; } push @st, $_; $hash{"$_"}--; } return 0; } sub returnword { my $l = shift; my @word = split //, $l; foreach( @word ) { if( $store{$_} ) { $hash{$_}++; } } return 0; }