Created
December 8, 2015 15:52
-
-
Save brianbenson/35302df292ee07f4daa7 to your computer and use it in GitHub Desktop.
December 2015 Scripting games puzzle entry for Brian Benson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# December 2015 Scripting games puzzle entry | |
# Brian Benson | |
# bbenson@helpsoft.com | |
$list = @" | |
1 Partridge in a pear tree | |
2 Turtle Doves | |
3 French Hens | |
4 Calling Birds | |
5 Golden Rings | |
6 Geese a laying | |
7 Swans a swimming | |
8 Maids a milking | |
9 Ladies dancing | |
10 Lords a leaping | |
11 Pipers piping | |
12 Drummers drumming | |
"@ | |
cls | |
# 1) split list into a collection of entries and sort by length | |
$sortedCollection = $list.split( "`n") | sort-object -property length | |
write-output "***** #1 Sorted collection by length *****`n" | |
write-output $sortedCollection | |
# 1) - Bonus split list into a collection and sort by length without numbers | |
$sortedCollectionNoNums = $list.split("`n") | foreach-object { $_.substring($_.indexof(' ') + 1) } | sort-object -property length | |
write-output "`n`n`n`n`n***** #1 Bonus - Sorted collection by length without numbers *****`n" | |
write-output $sortedCollectionNoNums | |
# 2) turn each line into a custom object | |
$customObjects = @() | |
$array = $list.split( "`n") | |
foreach ($s in $array) | |
{ | |
[int]$count, $item = $s.split(" ", 2) | |
$customObjects += new-object -TypeName PSCustomObject -Property @{ 'Count' = $count;'Item' = $item } | |
} | |
write-output "`n`n`n`n`n***** #2 - Turn each line into a custom object *****" | |
write-output $customObjects | |
# 3 what is the total number of all non bird related items | |
$birdlist = @("Partridge in a pear tree","Turtle Doves", "French Hens", "Calling Birds", "Geese a laying", "Swans a swimming") | |
$birds = 0 | |
write-output "`n`n`n`n`n***** #3 - What is the total number of all non bird related items *****`n" | |
foreach ( $item in $customObjects ) { if( $birdlist -match $item.item ) {$birds += $item.Count; "{0} {1}" -f $item.Count, $item.item} } | |
"Total number of all non bird items: {0}" -f $birds | |
# 4 what is the total count of all items | |
$allItemCnt = 0 | |
foreach ( $item in $customObjects ) { $allItemCnt += $item.Count } | |
write-output "`n`n`n`n`n***** #4 - What is the total count of all items *****`n" | |
"Total count of all items: {0}" -f $allitemCnt | |
# bonus - calculate nth triangle number for 12 -- formula is n(n + 1) / 2 where n is the day number | |
"`n`n`n`n`n***** BONUS - Calculate total number of items given over 12 days *****`n" | |
$cumulativeTotal = 0 | |
(1..12) | foreach { "Day {0, 3} Daily Total {1, 5}" -f [string]$_, [string](($_ * ($_ + 1)) / 2); $cumulativeTotal += ($_ * ($_ + 1)) / 2 } | |
"Grand total for all 12 days... {0}" -f $cumulativeTotal | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment