Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Brain Teaser: Use PowerShell to Solve This Math Word Puzzle

I came across this post, Brain Teaser: Solve This Math Word Puzzle 

and the following Puzzle is given there where the Letters need to be replaced by Numbers to make the expression correct :

Each letter stands for a 1-digit number.  No two letters may stand for the same number.  Find a value for each letter from the following set:  0, 1, 4, 5, 6, 7, 8, 9.

 

    ASK      

+  GAVE      

     -------      

   USED    

 

you can find the answer   here.  but I also made a Quick and Dirty  PowerShell script to solve this one :

# PowerShell Puzzle solver script
 
$n = 0,1,4,5,6,7,8,9
Foreach ($a in $n) {
  Foreach ($s in ($n -ne $a)) {
    Foreach ($k in ($n -ne $a -ne $s)) {
      Foreach ($g in ($n -ne $a -ne $s -ne $k)) {
        Foreach ($v in ($n -ne $a -ne $s -ne $k -ne $g)) {
          Foreach ($e in ($n -ne $a -ne $s -ne $k -ne $g -ne $v)) {
            Foreach ($u in ($n -ne $a -ne $s -ne $k -ne $g -ne $v -ne $e)) {
              Foreach ($d in ($n -ne $a -ne $s -ne $k -ne $g -ne $v -ne $e -ne $u)) {
                if ( ($a,$s,$k,$g,$v,$e,$u,$d).count -eq ($a,$s,$k,$g,$v,$e,$u,$d | sort -Unique).count ) {
                  #[string]($a,$s,$k,$g,$v,$e,$u,$d)
                  if (Invoke-Expression "$a$s$k + $g$a$v$e -eq $u$s$e$d") {
                    "ASK = $a$s$k"
                    "GAVE = $g$a$v$e"
                    "USED = $u$s$e$d"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This also gives me the answer given, but I actualy did find 2 correct answers to the given Puzzle this way, what is the other correct answer, can you figure that out before running the script ? ;-) 

Enjoy, 

Greetings /\/\o\/\/

 

Published Wednesday, April 04, 2007 10:47 AM by MoW
Filed under: , ,

Comments

# re: Brain Teaser: Use PowerShell to Solve This Math Word Puzzle

Hi Marc,

I also have a naive implementation of python code that gives me two solution:

{'A': 5, 'E': 7, 'D': 1, 'G': 8, 'K': 4, 'S': 0, 'U': 9, 'V': 6}

{'A': 7, 'E': 5, 'D': 1, 'G': 8, 'K': 6, 'S': 4, 'U': 9, 'V': 0}

Isaac

Wednesday, April 04, 2007 2:20 PM by Isaac Yuen

# re: Brain Teaser: Use PowerShell to Solve This Math Word Puzzle

Hi Isaac,

I did see your blog entry, and I got the same answers

You might be happy to know that PowerShell is available for XP and Vista allready.

Greetings /\/\o\/\/

PS. this line is not needed (anymore ;-) in my version, I forgot to remove it :

if ( ($a,$s,$k,$g,$v,$e,$u,$d).count -eq ($a,$s,$k,$g,$v,$e,$u,$d | sort -Unique).count )

Thursday, April 05, 2007 5:31 AM by MoW
Anonymous comments are disabled