Showing posts with label 2008 Scripting Games. Show all posts
Showing posts with label 2008 Scripting Games. Show all posts
Tuesday, March 4, 2008
Monday, March 3, 2008
2008 Scripting Games!! Beg. Event 10
The last event for the 2008 Scripting Games. Next year I'll be competing in the Advanced category. As for now here is my solution for Event 10.
Thats it for the 2008 Games! See you next year at the Advanced Events!
1: $arrFrames = 2,5,7,"/",8,1,"X",9,"/",5,3,7,0,4,5,"X",2,0
2: $score=03: for ($i=0;$i -le ($arrframes.length-1);$i++) {
4:
5: if (($arrFrames[$i] -eq "X") -and ($arrFrames[$i+1] -eq "X") -and ($arrFrames[$i+2] -eq "X")) {
6: $score+=30 7: }8: elseif (($arrFrames[$i] -eq "X") -and ($arrFrames[$i+1] -eq "X")) {
9: $score+=20 + ($arrFrames[$i+2]) 10: }11: elseif (($arrFrames[$i] -eq "X") -and ($arrFrames[$i+2] -eq "/")) {
12: $score+=20 13: }14: elseif ($arrFrames[$i] -eq "X") {
15: $score+=10 + ($arrFrames[$i+1] + $arrFrames[$i+2]) 16: }17: if (($arrFrames[$i] -eq "/") -and ($arrFrames[$i+1] -eq "X")) {
18: $score+=10 + (10 - ($arrFrames[$i-1])) 19: }20: elseif ($arrFrames[$i] -eq "/") {
21: $score+=(10 - ($arrFrames[$i-1])) + ($arrFrames[$i+1]) 22: }23: if ($arrFrames[$i] -is [int]) {
24: $score+=$arrFrames[$i] 25: }26:
27: }28: "My Score: $score"
Thats it for the 2008 Games! See you next year at the Advanced Events!
2008 Scripting Games!! Beg. Event 9
My Solution for Event 9 of the 2008 Scripting Games.
1: $mdb = "c:\Scripts\pool.mdb"
2: $ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$mdb"
3: $Conn = new-object System.Data.OleDb.OleDbConnection($connString) 4: $conn.open() 5: $cmd = new-object System.Data.OleDb.OleDbCommand("SELECT * from SwimmingPool",$Conn)
6: $da = new-Object System.Data.OleDb.OleDbDataAdapter($cmd) 7: $dt = new-Object system.Data.Datatable 8: [void]$da.Fill($dt) 9: $dt | % { 10: "Name: " + $_.customer
11: if($_.slope) {"Volume of Water:" + $_.Length * $_.Width * (($_.SStart + $_.SEnd) / 2)*1000 }
12: else {"Volume of Water:" + $_.Length * $_.Width * $_.Depth * 1000 }
13: }14:
15: $conn.close()Thursday, February 28, 2008
2008 Scripting Games!! Beg. Event 8
Event 7 was just an error checking event so no need to post on that. Event 8 though was pretty straightforward.
1: [int]$winningNumber=(New-Object random).Next(0,50)2:
3: function CheckIfWinner {
4: $guessCount++5: [int]$guessedNumber=Read-Host "Enter a number between 0 and 50"
6: if ($guessedNumber -eq $winningNumber) {"Random: $winningNumber `nTotalGuesses: $guessCount"}
7: elseif ($guessedNumber -lt $winningNumber) {Write-Host "Too Low" -ForegroundColor green;CheckifWinner}
8: else {Write-Host "Too High" -ForegroundColor red;CheckIfWinner}
9: } 10: CheckIfWinnerTuesday, February 26, 2008
2008 Scripting Games!! Beg. Event 6
Event 6 I got a little lazy and just did a few copy paste commands.
1: gc "C:\scripts\coffee.txt" | ? { $_.Contains("Espresso")} | % { $Espresso+=[int]$($_.substring($_.length-2,2)) };"Espresso: $espresso"
2: gc "C:\scripts\coffee.txt" | ? { $_.Contains("Latte")} | % { $Latte+=[int]$($_.substring($_.length-2,2)) };"Latte: $Latte"
3: gc "C:\scripts\coffee.txt" | ? { $_.Contains("Cappuccino")} | % { $Cappuccino+=[int]$($_.substring($_.length-2,2)) };"Cappuccino: $Cappuccino"
2008 Scripting Games!! Beg. Event 5
Event 5 was probably the second most difficult. My solution is not as elegant as some I've seen but it works. 8)
1: param ([datetime]$dateToCompare = $(Throw "Must specify a Date"))
2:
3: $DatetoCompareYear = $dateToCompare.year 4: $DateToCompareMonth = $dateToCompare.month 5: $days = (New-TimeSpan (get-date).tolongdatestring() $datetocompare.tolongdatestring()).days 6: $months = ((($dateToCompareYear - (get-date).Year) * 12) + ($dateToCompareMonth - (get-date).Month))7: "Days:$days"
8: "Months:$months"
9:
10: [int]$FullMonths = "{0:N0}" -f $((New-TimeSpan $(get-date) $datetocompare).days / 30.43)
11: [int]$Leftoverdays = "{0:N0}" -f $($days - ($fullmonths * 30.43))
12:
13: "Months/Days: $FullMonths / $leftoverDays"
Monday, February 25, 2008
Sudden Death Challenge Event 5
Event 5 for the Sudden Death Challenge was much more difficult than the previous events. I was able to solve it with less than 10 lines of code. Powershell is truly the future of MS admin scripting. My solution to Event 5 of the SD Challenge.
First extracting all the classes that begin with "Win32_" and saving each classes Properties into a variable. Here's the tricky part. We need to match the first letter of any class and build a list from "a" to "y". Hence the array 97..121. This is the ASCII code for "a" to "y". I then build an array of any class that matches the first letter of its property with A for the first loop and is from the Win32 class. Then only selecting the first element of that array I continue the loop from "a" to "y".
1: $list=gwmi -List |?{$_ -match "win32_"} | % {$_.PSBase.Properties }
2: 97..121 | % {3: $(for ($i=0;$i -lt $list.Length;$i++) {
4: if (($list[$i].name.substring(0,1) -eq [char]$_) -and ($list[$i].origin.substring(0,5) -eq "win32")) {
5: "$($list[$i].name)`t$($list[$i].origin)"}})[0]
6: }First extracting all the classes that begin with "Win32_" and saving each classes Properties into a variable. Here's the tricky part. We need to match the first letter of any class and build a list from "a" to "y". Hence the array 97..121. This is the ASCII code for "a" to "y". I then build an array of any class that matches the first letter of its property with A for the first loop and is from the Win32 class. Then only selecting the first element of that array I continue the loop from "a" to "y".
2008 Scripting Games!! Beg. Event 4
Event 4 was another easy one to do in Powershell. I did however receive "0" points even though I correctly solved the event. I emailed MS to see if they could provide an answer to why this happened. I did however get my points in the VBScript category. None the less here is my solution for Event 4.
I'm using the built in variable $myinvocation to get the whole path to the script and piping it to the measure-object cmdlet to get the count of Characters then passing that to the select cmdlet then finally to format-list.
1: gc ($myinvocation.MyCommand.path) | Measure -Character| select characters |flI'm using the built in variable $myinvocation to get the whole path to the script and piping it to the measure-object cmdlet to get the count of Characters then passing that to the select cmdlet then finally to format-list.
2008 Scripting Games!! Beg. Event 3
Event 3 for Powershell this was very simple. My VBScript solution was 14 lines long compared to my one liner in PS.
Listing all the files in the Scripts folder (LS) for all the text files. Then getting the first line and only the first line @(gc $_.fullname)[0] then piping that line to the Add-content cmdlet to build the new file. Learned that trick from Keith Hill. 8)
1: ls c:\Scripts | ? {$_.Extension -eq ".txt" } | % { @(Gc $_.fullname)[0] | ac c:\Temp\newfile.txt}
Listing all the files in the Scripts folder (LS) for all the text files. Then getting the first line and only the first line @(gc $_.fullname)[0] then piping that line to the Add-content cmdlet to build the new file. Learned that trick from Keith Hill. 8)
Sudden Death Challenge Event 4
I didn't compete in any of the first 3 Sudden Death Events as I didn't have enough time at work to even look at them. As time permitted I was however able to do the last few of them. Here is my solution to the Event 4 of the SD Challenge.
Pretty simple actually. Using the "for" statement to grab every 2 numbers which is the "ASCII Code" equivalent to the letters we want. The first 2 numbers "68" is the "ASCII Code" for "D". The next 2 numbers "82" is the "ASCII Code" for "R", and so on, and so on.
So this:
6882463283678273808479
Turns into this:
DR. SCRIPTO
1: $line=gc c:\Scripts\numbers.txt2: for ($i=0;$i -le 20; $i+=2) {$word+=[char][int]$line.Substring($i,2)}
3: $wordPretty simple actually. Using the "for" statement to grab every 2 numbers which is the "ASCII Code" equivalent to the letters we want. The first 2 numbers "68" is the "ASCII Code" for "D". The next 2 numbers "82" is the "ASCII Code" for "R", and so on, and so on.
So this:
6882463283678273808479
Turns into this:
DR. SCRIPTO
Thursday, February 21, 2008
2008 Scripting Games!! Beg. Event 2
My Powershell solution for Event 2. Its a bit different than MOW's solution (The things Marc does is mind boggling!!)
This was pretty straight forward and really doesnt need much explaining. Simply getting the key as an array and parsing through it looking for "TrueType" in the name with the Foreach statement.
1: $key = Get-Item 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts'
2: foreach ($subkeyname in $key.property) {
3: if ($subkeyname.contains("TrueType")) {
4: $subkeyname 5: $truetype++ 6: } 7: }8: ""
9: "TrueType: $truetype"
10: "Total: " + $key.Property.Length
This was pretty straight forward and really doesnt need much explaining. Simply getting the key as an array and parsing through it looking for "TrueType" in the name with the Foreach statement.
2008 Scripting Games!! Beg. Event 1
For the first year I've decided to enter the MS Scripting Games. I only entered the Beginners/Intermediate level so I can get a feel for the events. Half of the "Advanced" Events are very tough even for some of the best scripters. I think next year I will be there and give it a shot.
I entered the Games for both VBScript and Powershell. So, to avoid duplicate post I'll just post my Powershell solutions.
Now I know this can be condensed quite a bit with nested "For" statements this was the easiest way to get the results I needed and it's very easy to read.
I entered the Games for both VBScript and Powershell. So, to avoid duplicate post I'll just post my Powershell solutions.
1: $card1="Seven of spades"
2: $card2="Five of hearts"
3: $card3="Seven of diamonds"
4: $card4="Seven of clubs"
5: $card5="King of clubs"
6:
7: $valueOfCard1=$card1.substring(0,($card1.IndexOf(" ")))
8: $valueOfCard2=$card2.substring(0,($card2.IndexOf(" ")))
9: $valueOfCard3=$card3.substring(0,($card3.IndexOf(" ")))
10: $valueOfCard4=$card4.substring(0,($card4.IndexOf(" ")))
11: $valueOfCard5=$card5.substring(0,($card5.IndexOf(" ")))
12:
13: if ($valueOfCard1 -eq $valueOfCard2) { $pairs++;$result += "$card1 and $card2`n" }
14: if ($valueOfCard1 -eq $valueOfCard3) { $pairs++;$result += "$card1 and $card3`n" }
15: if ($valueOfCard1 -eq $valueOfCard4) { $pairs++;$result += "$card1 and $card4`n" }
16: if ($valueOfCard1 -eq $valueOfCard5) { $pairs++;$result += "$card1 and $card5`n" }
17: if ($valueOfCard2 -eq $valueOfCard3) { $pairs++;$result += "$card2 and $card3`n" }
18: if ($valueOfCard2 -eq $valueOfCard4) { $pairs++;$result += "$card2 and $card4`n" }
19: if ($valueOfCard2 -eq $valueOfCard5) { $pairs++;$result += "$card2 and $card5`n" }
20: if ($valueOfCard3 -eq $valueOfCard4) { $pairs++;$result += "$card3 and $card4`n" }
21: if ($valueOfCard3 -eq $valueOfCard5) { $pairs++;$result += "$card3 and $card5`n" }
22: if ($valueOfCard4 -eq $valueOfCard5) { $pairs++;$result += "$card4 and $card5`n" }
23:
24: "Pairs: $pairs"
25: $result
Now I know this can be condensed quite a bit with nested "For" statements this was the easiest way to get the results I needed and it's very easy to read.
Subscribe to:
Posts (Atom)