Scriptinggames2007

From Terminal23wiki

Jump to: navigation, search

This page is my summary of what I did and learned in the 2007 Winter Scripting Games. I participated in the PowerShell division and ended up entering both Beginner and Advanced divisions mostly because these mark my very first ever scripts and look at PowerShell. My goal was to give a best effort (about 50%) in the Advanced division and get most of the Beginner stuff completed. The scores show me exceeding my goals (95 in Beginner and 80 in Advanced) but my Advanced score really should have been 90 had it not been for some weirdness in the scoring (weird line breaks in my emails, etc). I didn't make a big deal about it, and really don't care about the raw score as much as I do the enjoyment I got from the whole experience.

I have also begun revisiting these events and redoing my answers to make them cleaner and better and smaller based on my new experience and other people's answers. I try my best not to fully lift them, but just be inspired by them. Redux


Advanced Event 1 answer mow's version Gaurhoth's answer Adminspotting post

$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

Advanced Event 2 answer mow's version Gaurhoth's answer Adminspotting post

$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)

Advanced Event 3 answer mow's version Gaurhoth's answer Adminspotting post

$a = Read-Host "Enter your dollars"
$a = $a -replace("\$","")
$a = $a -replace("\.","")
$a = 5000 - $a
$change = $a / 100
$change = "{0:N2}" -f $change

$tens = $a / 1000
$tens = [math]::truncate($tens)
$a = $a - $tens * 1000
$fives = $a / 500
$fives = [math]::truncate($fives)
$a = $a - $fives * 500
$ones = $a / 100
$ones = [math]::truncate($ones)
$a = $a - $ones * 100
$quarters = $a / 25
$quarters = [math]::truncate($quarters)
$a = $a - $quarters * 25
$dimes = $a / 10
$dimes = [math]::truncate($dimes)
$a = $a - $dimes * 10
$nickels = $a / 5
$nickels = [math]::truncate($nickels)
$a = $a - $nickels * 5
$pennies = $a / 1
$pennies = [math]::truncate($pennies)

Write-Host "Change returned: $change"
Write-Host "Tens: $tens"
Write-Host "Fives: $fives"
Write-Host "Ones: $ones"
Write-Host "Quarters: $quarters"
Write-Host "Dimes: $dimes"
Write-Host "Nickels: $nickels"
Write-Host "Pennies: $pennies"

Advanced Event 4 answer mow's version Gaurhoth's answer Adminspotting post

$a = Read-Host "Enter your year"
$a = $a -1900
$b = $a / 12
$b = [math]::truncate($b)
$b = $b * 12
$a = $a - $b

switch($a)
{
"0"{$answer="Rat"}
"1"{$answer="Ox"}
"2"{$answer="Tiger"}
"3"{$answer="Rabbit"}
"4"{$answer="Dragon"}
"5"{$answer="Snake"}
"6"{$answer="Horse"}
"7"{$answer="Goat"}
"8"{$answer="Monkey"}
"9"{$answer="Rooster"}
"10"{$answer="Dog"}
"11"{$answer="Pig"}
}
Write-Host $answer

Advanced Event 5 answer mow's version Gaurhoth's answer Adminspotting post

$adOpenStatic = 3
$adLockOptimistic = 3
$objConnection = New-Object -comobject ADODB.Connection
$objRecordset = New-Object -comobject ADODB.Recordset
$objConnection.Open("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = /scores.mdb")
$objRecordset.Open("Select * from Results",
$objConnection,$adOpenStatic,$adLockOptimistic)

####### START MEAN #######
$objRecordset.MoveFirst()
$i,$avg = 0

do {
$avg += $objRecordset.Fields.Item("Score").Value
$i++;$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

$avg = [math]::truncate($avg / $i)

####### START MIN #######
$objRecordset.MoveFirst()
$max = 0

do {
if ($objRecordset.Fields.Item("Score").Value -gt $max)
{ $max = $objRecordset.Fields.Item("Score").Value}
else { }
$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

####### START MAX #######
$objRecordset.MoveFirst()
$min = $max

do {
if ($objRecordset.Fields.Item("Score").Value -lt $min)
{ $min = $objRecordset.Fields.Item("Score").Value}
else { }
$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

####### START MODE #######
[int[]]$modearray = @()

for ($n=0;$n -le $max;$n++)
{$modearray += 0
}
$objRecordset.MoveFirst()

do {
$n = $objRecordset.Fields.Item("Score").Value
$modearray[$n] = $modearray[$n] + 1
$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

$modemax = 0

for ($n=0;$n -le $modearray.length;$n++)
{
if ($modearray[$n] -gt $modemax)
{ $mode = $n; $modemax = $modearray[$n]}
else { }
}

####### START MEDIAN #######
[int[]]$medianarray = @()

for ($n=0;$n -lt $i;$n++)
{$medianarray += 0}

$n = 0
$objRecordset.MoveFirst()

do {
$medianarray[$n] = $objRecordset.Fields.Item("Score").Value
$n++;$objRecordset.MoveNext()}
until ($objRecordset.EOF -eq $True)

$medianarray = $medianarray | sort
$median = $medianarray[$medianarray.length/2]

####### START OUTPUT #######
Write-host "Mean: $avg"
Write-host "Mode: $mode"
Write-host "Median: $median"
Write-Host "Highest score: $max"
Write-Host "Lowest score: $min"

$objRecordset.Close()
$objConnection.Close()

Advanced Event 6 answer mow's version Gaurhoth's answer Adminspotting post

This is the one event I didn't have an answer for.

Advanced Event 7 answer mow's version Gaurhoth's answer Adminspotting post

if ($args[0] -eq "e") {

$input = [string]::join([environment]::newline, (get-content -path Alice2.txt))
for($i=0;$i -lt $input.length;$i++)
{
[int[]]$a = $a + [int] $input[$i]
$a[$i] += 1
$e = $e + [char] $a[$i]
}

$encodedfile = New-Item -type file "Encoded.txt" -Force
Set-Content Encoded.txt $e

} elseif ($args[0] -eq "d") {

if (Test-Path Encoded.txt) {

$input2 = [string]::join([environment]::newline, (get-content -path Encoded.txt))
for($i=0;$i -lt $input2.length;$i++)
{
[int[]]$x = $x + [int] $input2[$i]
$x[$i] -= 1
$y = $y + [char] $x[$i]
}

$y

} else { Write-Host "Encoded.txt not found. You probably need to use argument 'e' //
first to encode a file."}

} else { Write-Host "Please provide an argument 'e' (to encode) or 'd' (to decode) " } 

Advanced Event 8 answer

I got full credit here, but click the answers link to see them.

Advanced Event 9 answer mow's version Gaurhoth's answer Adminspotting post

$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 answer mow's version Gaurhoth's answer Adminspotting post

[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!"}


Beginner Event 1 answer

[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"}

Beginner Event 2 answer

get-wmiobject [class] | get-member

Beginner Event 3 answer

$a1 = "monday", "MONDAY", "monday"
$a2 = "TUESDAY", "tuesday", "tuesday"
$a3 = "WEDNESDAY", "wednesday", "wednesday"
$a4 = "thursday", "thursday", "THURSDAY"
$a5 = "friday", "FRIDAY", "friday"

$x1a = [String]::Compare($a1[0],$a1[1],$False)
$x1b = [String]::Compare($a1[1],$a1[2],$False)
$x1c = [String]::Compare($a1[0],$a1[2],$False)
if ($x1a -eq 0){"a1: third"}
elseif ($x2b -eq 0){"a1: first"}
else{"a1: second"}

$x2a = [String]::Compare($a2[0],$a2[1],$False)
$x2b = [String]::Compare($a2[1],$a2[2],$False)
$x2c = [String]::Compare($a2[0],$a2[2],$False)
if ($x2a -eq 0){"a2: third"}
elseif ($x2b -eq 0){"a2: first"}
else{"a2: second"}

$x3a = [String]::Compare($a3[0],$a3[1],$False)
$x3b = [String]::Compare($a3[1],$a3[2],$False)
$x3c = [String]::Compare($a3[0],$a3[2],$False)
if ($x3a -eq 0){"a3: third"}
elseif ($x3b -eq 0){"a3: first"}
else{"a3: second"}

$x4a = [String]::Compare($a4[0],$a4[1],$False)
$x4b = [String]::Compare($a4[1],$a4[2],$False)
$x4c = [String]::Compare($a4[0],$a4[2],$False)
if ($x4a -eq 0){"a4: third"}
elseif ($x4b -eq 0){"a4: first"}
else{"a4: second"}

$x5a = [String]::Compare($a5[0],$a5[1],$False)
$x5b = [String]::Compare($a5[1],$a5[2],$False)
$x5c = [String]::Compare($a5[0],$a5[2],$False)
if ($x5a -eq 0){"a5: third"}
elseif ($x5b -eq 0){"a5: first"} else{"a5: second"}

Beginner Event 4 answer

get-service | where-object {$_.status-eq "running"} | format-table -property //
DisplayName, Status -auto

Beginner Event 5 answer mow's version

I got half credit on this and got oh so close to the answer.
I couldn't get from hex over to decimal code.

$r = "It was the best of times...you know the rest."
$a = $r.ToCharArray()
$h = @()
$v = @()

for ($i=0;$i -le $a.length;$i++){
$x = [int][char]$a[$i]
$h += "{0:X}" -f $x
}
$h

for ($i=0;$i -le $h.length;$i++){

# $y = [byte]$h[$i]
# $y = "{0:D}" -f $h[$i]
# $y = [Convert]::ToString($h[$i],16)
#this is the last part $y = [char][int]$h[$i]
$y
}

Beginner Event 6 answer

1. -eq
2. }
3. foreach
4. continue (although this can just be left blank too)
5. While
6. Switch

Beginner Event 7 answer

$error.clear()
$erroractionpreference = "SilentlyContinue"

######## START UNCHANGED CODE #########

$a = 5
$b = 6
$c = "seven"
$d = 8

$x = $a + $b
$x = $x + $c
$x = $x + $d

$x

######## END UNCHANGED CODE #########

if ($error.Count -gt 0)
{ Write-Host "An error has occurred." }

# This would display the errors, but not required
# for ($i=0;$i -lt $error.Count;$i++)
# { $error[$i] } 

Beginner Event 8 answer

$jacksingame = 10
$i = 1

do {
$jacks = 10
$bounces = 0

do { $bounces++;$bouncestotal++;$jacks -= ($i * 1); } until ($jacks -le 0)

$jackspickedup += 10
$i++
} until ($i -gt $jacksingame)

Write-host "Total jacks: $jackspickedup"
Write-host "Total pick-ups: $bouncestotal" 

Beginner Event 9 answer

$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 answer

This was just simple unscrambling.

Personal tools