Welcome to ThePowerShellGuy.com Sign in | Join | Help

Games 2008 Advanced Event 1 Solution

In next few posts, I'll be posting my solution for the Winter Scripting Games of 2008. Some I'll add comments. Some I won't :)

I found using Regex patterns far easier than the brute force method used by the Scripting Guys. See the code below:

 

$number = Read-Host "Enter Phone Number"

$telcomap = @("","","[abc]","[def]","[ghi]","[jkl]","[mno]","[prs]","[tuv]","[wxy]")

$number.GetEnumerator() | % -begin { [string]$s = "" } -process { [string]$s += [string]$telcomap[[int][string]$_] }

(gc "c:\scripts\wordlist.txt" | ? { $_ -match "^$($s)$" }) | Select-Object -First 1

Couple of things might need to be explained. First I used an array to hold the letter to number mappings. Then I generated a regex pattern based off of the number given. For example, with the number 7323464. The resulting regex pattern would be '[prs][def][abc][def][ghi][mno][ghi]'. Last line reads in the word list and compares each word with the regex pattern until a match is found (using the -match comparison operator).

 Gaurhoth

Published Wednesday, February 20, 2008 6:37 AM by Gaurhoth

Comments

Anonymous comments are disabled