Word Puzzle (spoiler)

Wyverex wyverex.cypher at gmail.com
Thu Aug 7 18:45:01 PDT 2008


import tango.io.Stdout;

//All 50 states, lowercase, no spaces!!
static char[][50] states =
["alabama","alaska","arizona","arkansas","california","colorado",
"connecticut","delaware","florida","georgia","hawaii","idaho",
"illinois","indiana","iowa","kansas","kentucky","louisiana",
"maine","maryland","massachusetts","michigan","minnesota",
"mississippi","missouri","montana","nebraska","nevada",
"newhampshire","newjersey","newmexico","newyork","northcarolina",
"northdakota","ohio","oklahoma","oregon","pennsylvania","rhodeisland",
"southcarolina","southdakota","tennessee","texas","utah","vermont",
"virginia","washington","westvirginia","wisconsin","wyoming"];

void main()
{
   foreach( state1; states )
   {
     foreach( state2;states )
     {
       if( state1 == state2 ) continue;

       int['z' - 'a' + 1] letters;  //store letters

       //get all the letters in each state name
       foreach(c; state1)
         letters[c - 'a']++;

       foreach(c; state2)
         letters[c - 'a']++;


       //now to find two states names we can make from these letters
       STATE3:
       foreach( state3; states)
       {
         if ( state3 == state1 || state3 == state2 )
           continue;
         auto lets = letters.dup;

         foreach(c; state3)
         {
           if( lets[c -'a'] )
             lets[c -'a']--;
           else
             continue STATE3;
         }

         //okay were good here!! next state!!
         STATE4:
         foreach(state4; states)
         {
           if ( state4 == state1 || state4 == state2 || state4 == state3 )
             continue;
           auto lets2 = lets.dup;

           foreach(c; state4)
           {
             if( lets2[c -'a'] )
               lets2[c -'a']--;
             else
               continue STATE4;
           }

           //yeah!!! now to test for left overs
           foreach(i;lets2)
             if(i != 0) continue STATE4;

           //Got it!
           Stdout.formatln( "{} + {} = {} + {}", state1, state2, state3, 
state4 );
         }
       }
     }
   }
}


More information about the Digitalmars-d-learn mailing list