Wednesday, February 20, 2008

2008 Scripting Games, Solution 1

Here's my solution -

# Advanced Event 1: Could I Get Your Phone Number?
# http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent1.mspx

$number=read-host "Enter phone number"
# Combine a pipeline with the foreach statement
# This is necessary as only the first match is needed and break is then used
# to abort the loop
# The pipeline picks words 7 characters long
foreach($word in type c:\scripts\wordlist.txt | ? { $_.length -eq 7 }) {
# Convert letters to numbers
if (($word -replace "[abc]","2" -replace "[def]","3" -replace "[ghi]","4" `
-replace "[jkl]","5" -replace "[mno]","6" -replace "[prs]","7" `
-replace "[tuv]","8" -replace "[wxy]","9") -eq $number ) {
# Emit result in uppercase
$word.toupper()
# Exit foreach loop
break
}
}

No comments: