scripting games day 1 and 2: feet are wet

This post is just about my time with PowerShell for the 2007 Winter Scripting Games. If you have no interest, I certainly won’t be upset if you skip this post. I’m posting only because this will mark my first exposure to PowerShell.

The games have begun. Event 1 in the Beginner’s section basically wanted the creation of a message box (dialogue box, pop up box, et al) that changed a few attributes and did something based on the return behavior. My code looked like this:

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
$answer = [System.Windows.Forms.MessageBox]::Show(“Do you want to continue?”, “Continue Processing”, “YesNo”, “Question”)
if ($answer -eq “yes”)
{echo “Yes – Processing will continue…”}
else
{echo “No – Processing stopped”}

Nothing really special there. The only bad part is that I wasn’t able to bring the pop up box to the front, although some Googling turned up that this is a known issue that can be solved with vbscript.

Beginner Event 2 was a series of questions, each scored independently. In one column were ten classes, and in a second column were ten properties. The question is to match the classes with their properties. Not so bad. While I could eyeball this list and likely get an easy 8 of 10 correct, a couple would have required guesses. However, there is an easier way to do this:

get-wmiobject [class] | get-member

Replace [class] with the class name and look through the list for the matching terms.

Now, because of my success in the Beginner Events, I thought I would try my hand at the Advanced Events, which are definitely more up my alley as long as they stay in the “logic” arena as opposed to knowing my way around all the various obscure commands and methods and objects in PowerShell that I don’t know yet.

Event A1 wanted to convert from Roman Numerals to regular numbers. This actually proved more interesting than I thought it would be, and my eventual program, while satisfying the requirements, would not hold up to invalid input, error checking, or additional roman numerals above M.

$r = Read-Host “Please enter a Roman numeral:”
$a1 = $r.ToCharArray()
$a2 = @()
$value = 0

for ($i=0;$i -le $a1.length;$i++){
Switch ($a1[$i]){
“M” {$a2 += 1000}
“D” {$a2 += 500}
“C” {$a2 += 100}
“L” {$a2 += 50}
“X” {$a2 += 10}
“V” {$a2 += 5}
“I” {$a2 += 1}
}
}

for ($i=0;$i -le $a2.length;$i++){
$v1 = $a2[$i-1]
$v2 = $a2[$i]
$v3 = $a2[$i+1]

if ($flip -eq 1){$value += ($v2 – $a2[$i-1]);$flip=0}
elseif ($v2 -ge $v3){$value += $v2}
else {$flip=1}
}

Write-Host $value

Event A2 was simpler even though I had more trouble finding the information I needed (syntax, really). Find the number that when multiplied by 3 would give the smallest answer that consists of nothing but 4s. The answer is 148, and while I could create a script to find this by iterating through every number by multiplying it by 3 and check if the answer is all 4s, or even start with 10 4s and divide them by 3 all the way down to the lowest, but I eventually decided I wanted to just build a string of growing 4s and check each one to see if it was evenly divisible. Sadly, when I submitted my entry I made the mistake and echoed out “444” instead of the needed “148.” D’oh! I was too excited about figuring this one out!

$a = @()
do {
$a += 4
$b = [String]::join(“”,$a)
$m = $b % 3
if ($m -eq 0){
$x = $b / 3
write-Host $x}
}
until ($m -eq 0)

Not too shabby there, I hope! So far, that is very encouraging and my goal has expanded to not just completing the Beginner section, but to complete at least half of the points from the Advanced.

One thought on “scripting games day 1 and 2: feet are wet

  1. You and Mow both used the same technique for Advanced Event 2.
    Also, instead of messing around with the array, $a += 4 as a string works, too.

Comments are closed.