Scriptinggames2008
From Terminal23wiki
Here are my solutions to events in the Microsoft 2008 Scripting Games. I participated using PowerShell in the Advanced and Sudden Death events. Scores for the Advanced Events are posted, as are the Sudden Death Events. Main score page.
$phonenumber = Read-Host "Enter your 7 digit phone number"
"Please hold just a few moments..."
$fullwordlist = get-content c:\scripts\wordlist.txt
[array]$shortwordlist = @()
foreach ($i in $fullwordlist)
{
if ($i.Length -eq 7)
{ $shortwordlist += $i.ToLower() }
else {}
$counter++
}
for ($z=0;$z -le 6;$z++)
{
$newshortwordlist = @()
switch ($phonenumber[$z])
{
"2" {[array]$possibleletters = "a,b,c".Split(",") }
"3" {[array]$possibleletters = "d,e,f".Split(",") }
"4" {[array]$possibleletters = "g,h,i".Split(",") }
"5" {[array]$possibleletters = "j,k,l".Split(",") }
"6" {[array]$possibleletters = "m,n,o".Split(",") }
"7" {[array]$possibleletters = "p,r,s".Split(",") }
"8" {[array]$possibleletters = "t,u,v".Split(",") }
"9" {[array]$possibleletters = "w,x,y".Split(",") }
default {"Something is wrong, only use digits 2-9!";exit}
}
foreach ($j in $shortwordlist)
{
if ($j[$z] -eq $possibleletters[0] -or $j[$z] -eq $possibleletters[1] -or $j[$z] -eq $possibleletters[2])
{ [array]$newshortwordlist += $j }
$counter++
}
$shortwordlist = $newshortwordlist
#$newshortwordlist.Count
}
#this is a vanity counter to tally how many interations the code does
#$counter
if ($shortwordlist -ge 1)
{ $shortwordlist[0] }
else { "no matches" }
Advanced Event 2 answer expert commentary
$skaters = get-content C:\scripts\skaters.txt
[array]$avgscores = @()
foreach ($i in $skaters)
{
[array]$data = $i.Split(",")
[int[]]$allscores = $data[1..($data.count - 1)] | sort-object -descending
$avgscore = ($allscores[1] + $allscores[2] + $allscores[3] + $allscores[4] + $allscores[5]) / 5
[string]$avgscoresline = "$avgscore,$($data[0])"
[array]$avgscores += $avgscoresline
}
$avgscores = $avgscores | sort-object -descending
Write-Host "Gold medal: $($avgscores[0].Split(`",`")[1]), $($avgscores[0].Split(`",`")[0])"
Write-Host "Silver medal: $($avgscores[1].Split(`",`")[1]), $($avgscores[1].Split(`",`")[0])"
Write-Host "Bronze medal: $($avgscores[2].Split(`",`")[1]), $($avgscores[2].Split(`",`")[0])"
function CleanVotes ($toremove)
{
$q = 0
do
{
$allvotes[$q] = $allvotes[$q].Replace("$toremove,","")
$allvotes[$q] = $allvotes[$q].Replace(",$toremove","")
$q++
}
Until ($q -eq $allvotes.count)
}
function TallyVotes
{
$kenmyer,$pilarackerman,$jonathanhaas,$syedabbas = 0
foreach ($i in $allvotes)
{
$i = $i.Split(",")
switch ($i[0])
{
"Ken Myer" {$kenmyer++}
"Jonathan Haas" {$jonathanhaas++}
"Syed Abbas" {$syedabbas++}
"Pilar Ackerman" {$pilarackerman++}
default {"danger, will robinson, danger!"}
}
}
$total = @()
if ($kenmyer -ne $null){$total += "$($kenmyer / $allvotes.count *100),Ken Myer"}
if ($jonathanhaas -ne $null){$total += "$($jonathanhaas / $allvotes.count *100),Jonathan Haas"}
if ($syedabbas -ne $null){$total += "$($syedabbas / $allvotes.count *100),Syed Abbas"}
if ($pilarackerman -ne $null){$total += "$($pilarackerman / $allvotes.count *100),Pilar Ackerman"}
return $total
}
#BEGIN MAIN!
$Global:allvotes = get-content C:\scripts\Votes.txt
do
{
$total = TallyVotes | sort-object -descending
#$total
if ($total[0].Split(",")[0] -le 50)
{
# we need to keep voting!
$count = $total.count - 1
$remove = $total[$count].Split(",")[1]
CleanVotes $remove
}
else
{
Write-Host "The winner is $($total[0].Split(`",`")[1]) with $($total[0].Split(`",`")[0])% of the vote."
$winner = $true
}
} Until ($winner -eq $true)
$submitteddate = Read-Host "Enter month/year"
$submitteddate = $submitteddate.Split("/")
[array]$a = (get-date -month $submitteddate[0] -year $submitteddate[1] -day 1 -uformat "%B %Y %u").Split(" ")
# format: month year day#
$daysinmonth = 0
switch ($a[0])
{
"January" {$daysinmonth = 31}
"February" {$daysinmonth = 28}
"March" {$daysinmonth = 31}
"April" {$daysinmonth = 30}
"May" {$daysinmonth = 31}
"June" {$daysinmonth = 30}
"July" {$daysinmonth = 31}
"August" {$daysinmonth = 31}
"September" {$daysinmonth = 30}
"October" {$daysinmonth = 31}
"November" {$daysinmonth = 30}
"December" {$daysinmonth = 31}
default {"that's no moon; it's a space station!"}
}
if ($a[0] -eq "February" -and $a[1]%4 -eq 0)
{ $daysinmonth = 29 }
Write-Host ""
Write-Host "$($a[0]) $($a[1])"
Write-Host ""
Write-Host "Su Mo Tu We Th Fr Sa"
[int]$startday = $a[2]
for ($x=1;$x -le $daysinmonth;$x)
{
$newline = $null
for ($y=0;$y -le 6;$y++)
{
if ($y -lt $startday){ $newline += " " }
else
{
if ($x -lt 10){ $newline += " " }
if ($x -le $daysinmonth){ $newline += "$x" }
$x++
}
$newline += " "
}
$startday = 0
$newline
}
function CheckAgainstWordList ($password)
{
$q = 0
$pass = $true
do
{
if ($password.ToLower() -eq $wordlist[$q].ToLower())
{ $pass = $false }
$q++
} Until ($q -eq $wordlist.Count -or $pass -eq $false)
return $pass
}
############# BEGIN MAIN
if ($args[0]){} else { write-host "provide a password as an argument";exit }
[string]$password = $args[0]
[array]$testsfailed = @()
[int]$passwordscore = 13
$wordlist = get-content C:\scripts\wordlist.txt
############# CHECK WORD
$checkresult = CheckAgainstWordList $password
if ($checkresult -eq $false)
{
$testsfailed += "Password is a word."
$passwordscore = $passwordscore -1
}
############# CHECK WORD MINUS LAST CHAR
$checkresult = CheckAgainstWordList $password.Remove($password.Length-1)
if ($checkresult -eq $false)
{
$testsfailed += "Password is a word followed by a character."
$passwordscore = $passwordscore -1
}
############# CHECK WORD MINUS FIRST CHAR
$password2 = $password.Remove(0,1)
$checkresult = CheckAgainstWordList $password2
if ($checkresult -eq $false)
{
$testsfailed += "Password is a word preceded by a character."
$passwordscore = $passwordscore -1
}
############# CHECK ZERO SUBBED FOR O
if ($password -match "0")
{
$password2 = $password.Replace("0","o")
$checkresult = CheckAgainstWordList $password2
if ($checkresult -eq $false)
{
$testsfailed += "Password is a word with O replaced with zero."
$passwordscore = $passwordscore -1
}
}
############# CHECK 1 SUBBED FOR L
if ($password -match "1")
{
$password2 = $password.Replace("1","L")
$checkresult = CheckAgainstWordList $password2
if ($checkresult -eq $false)
{
$testsfailed += "Password is a word with L replaced with one."
$passwordscore = $passwordscore -1
}
}
############# CHECK LENGTH
if ($password.Length -ge 10 -and $password.Length -le 20){ }
else
{
$testsfailed += "Password is not at least 10 characters and less than or equal to 20 characters."
$passwordscore = $passwordscore -1
}
############# CHECK INCLUSION OF ONE LOWER CASE, UPPER CASE, SYMBOL, 4 LETTERS IN A ROW
$numberpass = $false
$lowercase = $false
$uppercase = $false
$symbol = $false
$lowercounter = 0
$uppercounter = 0
$lowercounterfail = $false
$uppercounterfail = $false
for ($m=0;$m -le $password.Length-1;$m++)
{
$checkchar = [int]$password[$m]
if ($checkchar -ge 97 -and $checkchar -le 122)
{
$lowercase = $true
$lowercounter++
$uppercounter = 0
}
else
{
if ($checkchar -ge 65 -and $checkchar -le 90)
{
$uppercase = $true
$lowercounter = 0
$uppercounter++
}
else
{
if ($checkchar -ge 48 -and $checkchar -le 57)
{
$numberpass = $true
$lowercounter = 0
$uppercounter = 0
}
else
{
$symbol = $true
$lowercounter = 0
$uppercounter = 0
}
}
}
if ($lowercounter -ge 4){$lowercounterfail = $true}
if ($uppercounter -ge 4){$uppercounterfail = $true}
}
if ($numberpass -eq $false)
{
$testsfailed += "No numbers in password."
$passwordscore = $passwordscore -1
}
if ($lowercase -eq $false)
{
$testsfailed += "No lowercase letters in password."
$passwordscore = $passwordscore -1
}
if ($uppercase -eq $false)
{
$testsfailed += "No uppercase letters in password."
$passwordscore = $passwordscore -1
}
if ($symbol -eq $false)
{
$testsfailed += "No symbols in password."
$passwordscore = $passwordscore -1
}
if ($lowercounterfail -eq $true)
{
$testsfailed += "Four consecutive lowercase letters in password."
$passwordscore = $passwordscore -1
}
if ($uppercounterfail -eq $true)
{
$testsfailed += "Four consecutive uppercase letters in password."
$passwordscore = $passwordscore -1
}
############# CHECK FOR DUPLICATE CHARS
$duplicatefail = $false
for ($g=0;$g -le $password.Length-1;$g++)
{
$checkchar = [int]$password[$g]
$duplicatecounter = 0
for ($h=0;$h -le $password.Length-1;$h++)
{
$checkchar2 = [int]$password[$h]
if ($checkchar2 -eq $checkchar)
{ $duplicatecounter++ }
}
if ($duplicatecounter -ge 2)
{ $duplicatefail = $true }
}
if ($duplicatefail -eq $true)
{
$testsfailed += "Duplicate letters in password."
$passwordscore = $passwordscore -1
}
############# OUTPUT
$testsfailed
Write-Host ""
if ($passwordscore -le 6)
{ Write-Host "A password score of $passwordscore indicates a weak password." }
elseif ($passwordscore -le 10)
{ Write-Host "A password score of $passwordscore indicates a moderately-strong password." }
else
{ Write-Host "A password score of $passwordscore indicates a strong password." }
#find some prime numbers
$numberlist = 2..199
$survivornumbers = New-Object System.Collections.ArrayList
$candidatenumbers = 1..200
for($y=2;$y -le 200;$y++)
{ $survivornumbers.Add($y) | out-null }
foreach ($x in $numberlist)
{
foreach ($i in $candidatenumbers)
{
$counter++
if ($i -gt $x)
{
if ($i % $x -eq 0)
{ $survivornumbers.Remove($i) }
}
}
$candidatenumbers = @()
foreach ($t in $survivornumbers)
{$candidatenumbers += $t}
}
$survivornumbers
#$counter
$teams = "A,B,C,D,E,F".Split(",")
$games = New-Object System.Collections.ArrayList
foreach ($team in $teams)
{
$x++
for($y=$x;$y -le 5;$y++)
{
$gameline = "$team vs $($teams[$y])"
$games.Add($gameline) | out-null
}
}
$randgames = $games |% {$R = new-object random}{$R.next(0,$games.count) |%{$games[$_];$games.removeat($_)}}
$randgames
$MasterSongList = Get-Content C:\Scripts\SongList.csv
$BurnList = @()
$rand = new-object random
$BurnListTime = new-timespan
$MinTime = new-timespan -minute 75
$MaxTime = new-timespan -minute 80
#need to figure out what our list of songs will be
do
{
$randompick = $rand.next(0,$MasterSongList.count)
$nextsongpick = $MasterSongList[$randompick]
$checksong = $nextsongpick.Split(",")
$duplicate = $false
$TimeExceeded = $false
$dupeartistcounter = 0
#eliminate duplicate songs and no more than 2 of same artist
foreach ($songline in $BurnList)
{
if ($nextsongpick -eq $songline)
{$duplicate = $true}
else {}
if($nextsongpick.Split(",")[0] -eq $songline.Split(",")[0])
{$dupeartistcounter++}
if ($dupeartistcounter -ge 2)
{$duplicate = $true}
}
#evaluate our total time to get over 75 minutes but not exceed 80 minutes
if ($duplicate -eq $false)
{
$SongTime = $nextsongpick.Split(",")[2]
$SongTime = new-timespan -minute $SongTime.Split(":")[0] -seconds $SongTime.Split(":")[1]
$TestTime = $BurnListTime + $SongTime
if ($TestTime -le $MinTime)
{
$TimeCounter = 0
}
elseif ($TestTime -gt $MaxTime)
{
$TimeExceeded = $true
$TimeCounter++
}
elseif ($TestTime -gt $MinTime -and $TestTime -lt $MaxTime)
{
$TimeLimit = $true
}
}
#this won't happen with the scripting games data set, but there is the potential to have only songs
#that are 5+ minutes in length left over; which will cause an infinite loop. I've managed this with a counter
#on the do loop, but even if this happens, let's make the script robust enough to recover. We'll remove the last song
#which should free up space to recover.
if ($TimeCounter -ge 5)
{ $BurnList = $BurnList[0..($a.length - 2)] }
if ($duplicate -eq $false -and $TimeExceeded -eq $false )
{
$BurnList += $nextsongpick
$BurnListTime = $BurnListTime + $SongTime
#$nextsongpick
#"Total music time: $($SongTime.hours):$($SongTime.minutes):$($SongTime.seconds)"
#"Total music time: $($BurnListTime.hours):$($BurnListTime.minutes):$($BurnListTime.seconds)"
#"------"
}
$counter++
}Until ($TimeLimit -eq $true -or $counter -ge 100)
#time to display our results!
$BurnList = $BurnList | Sort
$MaxArtist = 0
$MaxSong = 0
#let's be nice and make pretty formatting
foreach ($BurnLine in $BurnList)
{
if ($BurnLine.Split(",")[0].Length -gt $MaxArtist)
{$MaxArtist = $BurnLine.Split(",")[0].Length}
if ($BurnLine.Split(",")[1].Length -gt $MaxSong)
{$MaxSong = $BurnLine.Split(",")[1].Length}
}
#let's be even nicer and number our list, to better see how it changes
foreach ($BurnLine in $BurnList)
{
$OutputSongNumber++
$OutputArtist = $BurnLine.Split(",")[0]
$OutputSong = $BurnLine.Split(",")[1]
$OutputTime = $BurnLine.Split(",")[2]
do
{
$OutputArtist += " "
} Until ($OutputArtist.Length -eq $MaxArtist+1)
do
{
$OutputSong += " "
} Until ($OutputSong.Length -eq $MaxSong+1)
Write-Host "$(`"{0:00}`" -f $OutputSongNumber) $OutputArtist$OutputSong$OutputTime"
}
Write-Host ""
Write-Host "Total music time: $($($BurnListTime.hours)*60+$($BurnListTime.minutes)):$(`"{0:00}`" -f $($BurnListTime.seconds))"
function Reverser ($string) {
for($x=$string.Length-1;$x -ge 0;$x--)
{ $reversedstring = $reversedstring + $string[$x] }
return $reversedstring
}
$AliceText = (Get-Content C:\Scripts\Alice.txt).Split(" ")
foreach ($word in $AliceText)
{ $AliceText2 = $AliceText2 + (Reverser $word) + " " }
Write-Host $AliceText2
function DealACard ($cardnumber)
{
$Global:cardsdealt = $Global:cardsdealt + 1
return $FullDeckShuffled[$cardnumber]
}
function GetCardValue ($card)
{
$card = $card.Split(" ")
switch ($card[0].ToLower())
{
"ace" {$cardvalue = 11}
"two" {$cardvalue = 2}
"three" {$cardvalue = 3}
"four" {$cardvalue = 4}
"five" {$cardvalue = 5}
"six" {$cardvalue = 6}
"seven" {$cardvalue = 7}
"eight" {$cardvalue = 8}
"nine" {$cardvalue = 9}
"ten" {$cardvalue = 10}
"jack" {$cardvalue = 10}
"queen" {$cardvalue = 10}
"king" {$cardvalue = 10}
default {Write-Host "two meters? that's impossible!"}
}
return $cardvalue
}
#start by creating our standard deck of cards
$suits = "clubs,spades,hearts,diamonds".Split(",")
$values = "ace,two,three,four,five,six,seven,eight,nine,ten,jack,king,queen".Split(",")
$FullDeck = New-Object System.Collections.ArrayList
for($x=0;$x -le $suits.Count -1;$x++)
{
for($y=0;$y -le $values.Count -1;$y++)
{
$FullDeck.Add($values[$y] + " of " + $suits[$x]) | out-null
}
}
#now shuffled that deck up randomly
$FullDeckShuffled = $FullDeck |% {$R = new-object random}{$R.next(0,$FullDeck.count) |%{$FullDeck[$_];$FullDeck.removeat($_)}}
#$FullDeckShuffled
#start the game!
$Global:cardsdealt = 0
$dealerscards = @()
$playerscards = @()
#deal the first two cards to player
$newcard = DealACard $cardsdealt
$playertotal += GetCardValue $newcard
$playerscards += $newcard
$newcard = DealACard $cardsdealt
$playertotal += GetCardValue $newcard
$playerscards += $newcard
#deal the first card to dealer
$newcard = DealACard $cardsdealt
$dealerscards += $newcard
$dealertotal += GetCardValue $newcard
do
{
Write-Host "-------------------------"
if ($action -eq "h")
{
$newcard = DealACard $cardsdealt
$playertotal += GetCardValue $newcard
$playerscards += $newcard
if ($playertotal -gt 21)
{ $action = "bust" }
}
Write-Host "Your cards:" -fore white
$playerscards
Write-Host ""
Write-Host "Dealer's cards:" -fore white
$dealerscards
Write-Host ""
if ($action -ne "bust")
{ $action = Read-Host "Stay (s) or hit (h)?" }
Write-Host ""
} Until ($action -eq "s" -or $action -eq "bust")
Write-Host "You have $playertotal." -fore white
if ($action -ne "bust")
{
Write-Host ""
Write-Host "Dealer's cards:" -fore white
$dealerscards
}
do
{
if ($action -ne "bust")
{
if ($dealertotal -lt $playertotal)
{
$newcard = DealACard $cardsdealt
$newcard
$dealertotal += GetCardValue $newcard
}
elseif ($dealertotal -eq $playertotal)
{
$stop = $true
$winstatus = "dealertie"
}
elseif ($dealertotal -gt $playertotal -and $dealertotal -le 21)
{
$stop = $true
$winstatus = "dealer"
}
elseif ($dealertotal -gt 21)
{
$winstatus = "dealerbust"
$stop = $true
}
}
} Until ($stop -eq $true -or $action -eq "bust")
Write-Host ""
switch ($winstatus)
{
"dealer" {Write-Host "Dealer has $dealertotal. Dealer wins." -fore white}
"dealertie" {Write-Host "Dealer has $dealertotal. Dealer wins tie." -fore white}
"dealerbust" {Write-Host "Dealer has $dealertotal. Dealer is bust. Player wins!" -fore white}
"" {Write-Host "Dealer wins." -fore white}
default {"there can be only one"}
}
I did not do this one.
Sudden Death Event 2 answer
$BlockofText = Get-Content C:\Scripts\Vertical.txt
$x = 0
do
{
foreach ($line in $BlockofText)
{$FixedString += $line[$x]}
$x++
} Until ($x -eq 4)
Write-Host $FixedString
Sudden Death Event 3 answer
$prezlist = Get-Content C:\scripts\Presidents.txt
$totalfirstname = 0
[string]$firstnamewinner = ""
$totalvowels = 0
$initials = @()
foreach ($prez in $prezlist)
{
#find the longest first name
$prez = $prez.Split(",")
if ($prez[1].Trim().Length -gt $totalfirstname)
{
$totalfirstname = $prez[1].Trim().Length
$firstnamewinner = $prez[1].Trim() + " " + $prez[0].Trim()
}
#evalute the vowel situation
[string]$prezstring = $prez[1].Trim().ToLower() + $prez[0].Trim().ToLower()
for ($x=0;$x -lt $prezstring.Length;$x++)
{
switch ($prezstring[$x])
{
"a" {$totalvowels++}
"e" {$totalvowels++}
"i" {$totalvowels++}
"o" {$totalvowels++}
"u" {$totalvowels++}
default {}
}
}
#grab all the first initials
$initials += $prez[0].Trim()[0]
$initials += $prez[1].Trim()[0]
}
#let's fiddle with the initial list to get the uniques
$initials = $initials | sort
$initials = $initials | group
foreach ($meh in $initials)
{ $uniqueinitials += $meh.Name }
#now let's figure out which ones are missing, the messy way
$unusedletters = New-Object System.Collections.ArrayList
$allletters = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".Split(",")
for($r=0;$r -le $allletters.Count;$r++)
{ $unusedletters.Add($allletters[$r]) | out-null }
for ($y=0;$y -lt $uniqueinitials.Length;$y++)
{ $unusedletters.Remove("$($uniqueinitials[$y])") }
[string]$initials = ""
foreach ($item in $unusedletters)
{ $initials += $item }
Write-Host "Longest first name: $firstnamewinner"
Write-Host "These letters are not used as an initial: $initials"
Write-Host "Total vowels used: $totalvowels"
Sudden Death Event 4 answer
$numbers = get-content C:\scripts\Numbers.txt
[int[]]$code = @()
for($x=0;$x -le $numbers.Length;$x++)
{
$code += $numbers[$x] + $numbers[$x+1]
$x++
}
foreach ($char in $code)
{ $string += [char]$char }
$string
Sudden Death Event 5 answer
I did not do this one.
Sudden Death Event 6 answer
$newtext = Get-Content C:\scripts\Lettercase.txt
[string]$fixedtext = $null
for($x=0;$x -le $newtext.Length;$x++)
{
if ($newtext[$x] -match "[0-9]")
{ $fixedtext += [int][string]$newtext[$x] -1 }
elseif ($newtext[$x] -cmatch "[A-Z]")
{ $fixedtext += $([string]$newtext[$x]).ToLower() }
elseif ($newtext[$x] -cmatch "[a-z]")
{ $fixedtext += $([string]$newtext[$x]).ToUpper() }
else{ $fixedtext += $newtext[$x] }
}
$fixedtext
Sudden Death Event 7 answer
$x = 100
$x = [math]::truncate($x*2/30)
$x = [math]::pow($x,5) *4
$x = [math]::sqrt($x) / 45
$x = "{0,0:n2}" -f $x
$speaker = new-object -com SAPI.SpVoice
$speaker.Speak("The answer is 42... I mean")
$speaker.Speak($x)
Sudden Death Event 8 answer
I did not do this one.
Sudden Death Event 9 answer
$newtext = Get-Content C:\scripts\Symbols.txt
[string]$fixedtext = $null
for($x=0;$x -le $newtext.Length;$x++)
{
if ($newtext[$x] -match "[A-Za-z0-9 ]")
{ $fixedtext += $newtext[$x] }
else{ }
}
$fixedtext
Sudden Death Event 10 answer
I did not do this one.
