Skip to content

Instantly share code, notes, and snippets.

@brianbenson
Created December 8, 2015 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianbenson/35302df292ee07f4daa7 to your computer and use it in GitHub Desktop.
Save brianbenson/35302df292ee07f4daa7 to your computer and use it in GitHub Desktop.
December 2015 Scripting games puzzle entry for Brian Benson
# 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