Created
December 6, 2015 20:48
-
-
Save sqlchow/cdc16aa9087eed0d5608 to your computer and use it in GitHub Desktop.
December scripting games puzzle - http://powershell.org/wp/2015/12/05/december-2015-scripting-games-puzzle/
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
$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 | |
"@ | |
$birdCollection = @("Birds","Doves", "Geese", "Hens", "Partridge","Swans") | |
$verseCollection = $list -split "`n" | ForEach-Object -Process { | |
$dayOfXmas = New-Object -TypeName psobject | |
if($_ -match "(\d+\s+)([\s\S]+)"){ | |
$dayOfXmas | | |
Add-Member -MemberType NoteProperty -Name Day -Value $Matches[1] -PassThru | | |
Add-Member -MemberType NoteProperty -Name Lyric -Value $Matches[2] -PassThru | |
} | |
} | |
$verseCollection | Select-Object -Property Day, Lyric, @{l='Length';e={$_.Lyric.Length}}| | |
Sort-Object -Property Length | Select-Object -Property Lyric | |
$countBirdsRef = 0 | |
$verseCollection | ForEach-Object {$_.Lyric -split "\s+"} | | |
ForEach-Object -Process { | |
if($birdCollection -contains $_){$countBirdsRef++} | |
} | |
$cumulativeGiftCount = 0 | |
$intrnlIterator = 0 | |
$verseCollection.Day | ForEach-Object -Process { | |
$intrnlIterator += ($_ -split "\s" | Measure-Object -Sum).Sum | |
$cumulativeGiftCount += $intrnlIterator | |
} | |
"*"*50 | |
"Bird References = {0}" -f $countBirdsRef | |
"Total count = {0}" -f ($verseCollection | Measure-Object -Property Day -Sum).Sum | |
"Cumulative Gift Count = {0}" -f $cumulativeGiftCount |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment