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\/\/