winter scripting games: events 9 and 10

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.

So, these are the final scripting games events! I’m actually enthused because I finished all four of these over 24 hours early and am very happy with the results.

Beginner event 9 wanted a list to be read and only certain values displayed from those lists. I ended up using the same code for each list. I assumed the first entry was a value I always wanted, and then any entry after a blank line was another one that I wanted.

$firstline = 1
$names = @()

foreach ($i in Get-Content List.txt){
if ($firstline -eq 1){$names+=$i;$firstline=0}else { }
if ($switch){$names+=$i;$switch=0}else { }
if($i){ }else {$switch=1}
}
Write-Host “—————“
Write-Host “List.txt names:”
$names

$firstline = 1
$names = @()

foreach ($i in Get-Content List2.txt){
if ($firstline -eq 1){$names+=$i;$firstline=0}else { }
if ($switch){$names+=$i;$switch=0}else { }
if($i){ }else { $switch=1 }
}
Write-Host “—————“
Write-Host “List2.txt names:”
$names
Write-Host “—————“

Beginner event 10 only wanted to have a bunch of terms unscrambled. This was rather easy and not even worth posting the scores here.

Advanced event 9 was an awesome little challenge. There were really two parts to this for me. First, figure out how to create a string and then force it to be valued like an expression. Eventually someone on IRC clued me into “invoke-expression” which was exactly what I was looking for. Second, figure out how to iterate through all 4 signs in all 4 places in the challenge equation. Basically, 4 nested loops. Here’s the surprisingly short code:

$signs = “+”,”-“,”*”,”/”

foreach($a in $signs){
foreach($b in $signs){
foreach($c in $signs){
foreach($d in $signs){

$equation += “12”+$a+”8″+$b+”4″+$c+”2″+$d+”9″
$guess = invoke-expression $equation

if($guess -eq 23){Write-Host “The answer is $equation”;exit}else { }

$equation = “”
}
}
}
}

Advanced event 10 introduced something new for me: colors! Holy crap, I can change the display colors! Now THIS can get fun! I also had to learn how to create a random number generator and be able to pull items out of an array without duplicating any. I think there were a number of ways to do this, but this method was the one I chose to tackle. Thankfully, it all worked out!

[collections.arraylist]$a = 1..20
$r = $a |% {$R = new-object random}{$R.next(0,$a.count) |%{$a[$_];$a.removeat($_)}}
for($i=0; $i -lt $r.count;$i++){
if ($r[$i] -le 5){$r[$i] = “BLUE”}
elseif($r[$i] -le 10){$r[$i] = “GREEN”}
elseif($r[$i] -le 15){$r[$i] = “RED”}
elseif($r[$i] -le 20){$r[$i] = “YELLOW”}
else {Write-Host “Error: round $i value $r[$i]”}
}

$score = 0

for($i=0; $i -lt $r.count;$i++){

$guess = Read-Host “Guess the next color (R, B, G, or Y)”

Switch($guess)
{
“R”{$guess = “RED”}
“B”{$guess = “BLUE”}
“Y”{$guess = “YELLOW”}
“G”{$guess = “GREEN”}
}

Write-Host $r[$i] -fore $r[$i]

if ($guess -eq $r[$i]){Write-Host “yay!”;$score++}
else {Write-Host “boo!”}

$total++
Write-Host “You have gotten $score out of $total correct.”
}

if ($score -ge 6){ Write-Host “YAY! You win teh prize! You have ESP!” -back “magenta” -fore “DarkBlue” }
else { Write-Host “boo! you lose! your guesses suck!”}

Scores to Advanced and Beginner divisions are posted.

One thought on “winter scripting games: events 9 and 10

Comments are closed.