scripting games 2007 events 9 and 10
Event 9
Event 9 gave an equation in which you had to determine the correct operators. Nested foreach loops and invoke-expression to the rescue!
$answer = 23
$operator = "+","-","*","/"
foreach ($op1 in $operator) {
foreach ($op2 in $operator) {
foreach ($op3 in $operator) {
foreach ($op4 in $operator) {
$exp = "12 $op1 8 $op2 4 $op3 2 $op4 9"
$a = invoke-expression $exp
if ($a -eq $answer) {
"12 $op1 8 $op2 4 $op3 2 $op4 9 = $answer"
exit
}
}
}
}
}
Event 10
This was a tricky little event. My code is horrible, but it was the last event and I waited until the last minute to get it done. Courtesy of a comment on Mow's old blog, I was able to get a nice card shuffle.
$r = new-object random
$correct = 0
$tries = 0
$cards = @{
1 = "B";
2 = "B";
3 = "B";
4 = "B";
5 = "B";
6 = "Y";
7 = "Y";
8 = "Y";
9 = "Y";
10 = "Y";
11 = "R";
12 = "R";
13 = "R";
14 = "R";
15 = "R";
16 = "G";
17 = "G";
18 = "G";
19 = "G";
20 = "G";
}
$nums = 1..20 | % { $in = $_; 1 | select @{n='num';e={$in}}, `
@{n='val';e={$r.Next()}} } | sort val | select num
foreach ($i in $nums) {
$tries += 1
$choice = read-host "Please predict the next color"
$card = $cards[$i.num]
if ($choice -eq $card) {
$correct += 1
}
switch ($card) {
"B" { "BLUE" | write-host -foregroundcolor blue }
"Y" { "YELLOW" | write-host -foregroundcolor yellow }
"R" { "RED" | write-host -foregroundcolor red }
"G" { "GREEN" | write-host -foregroundcolor green }
}
""
"$correct out of $tries tries"
""
}
if ($correct -ge 6) {
"The test is complete. You have ESP"
} else {
"The test is complete. You do not have ESP"
}
And that's it for the Games! This was definitely a fun event. It was really nice seeing everyone play.
