Last active
December 28, 2015 11:09
-
-
Save githubbery/d2ba4b0252a53bb000ce to your computer and use it in GitHub Desktop.
2015-December 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 | |
"@ | |
<# | |
1.Split $list into a collection of entries, as you typed them, and sort the results by length. | |
As a bonus, see if you can sort the length without the number. | |
#> | |
$collection = ((($list -split '[0-9]') | Select-String -Pattern [\S]) -replace "`n").trimstart() | |
$collectionSorted = $collection | sort -Property length | |
[int]$index = 0 | |
foreach ($object in $collectionSorted) | |
{ | |
$index = $index + 1 | |
Write-Host "$index " -NoNewline -ForegroundColor Cyan; Write-Host "$object" | |
} | |
<# | |
2.Turn each line into a custom object with a properties for Count and Item. | |
#> | |
$customCollection = @() | |
[int]$countValue = 0 | |
foreach ($line in $collection) | |
{ | |
$countValue = $countValue + 1 | |
$custObject = New-Object psobject -Property @{Count=$countValue; Item=$line} | |
$customCollection += $custObject | |
} | |
$customCollection | |
<# | |
3.Using your custom objects, what is the total number of all bird-related items? | |
#> | |
$birds = "Partridge","Turtle Doves","French Hens","Calling Birds","Geese","Swans" | |
[int]$birdRelatedItems = 0 | |
foreach ($row in $customCollection) | |
{ | |
if ($row.Item -match $birds[0] -or $row.Item -match $birds[1] -or $row.Item -match $birds[2] -or $row.Item -match $birds[3] -or $row.Item -match $birds[4] -or $row.Item -match $birds[5] ) | |
{ | |
$birdRelatedItems += [int]($row.count) | |
} | |
} | |
Write-Host "There are " -NoNewline; Write-Host "$birdRelatedItems" -ForegroundColor Green -NoNewline; Write-Host " bird-related items" | |
<# | |
4.What is the total count of all items? | |
#> | |
[int]$itemCount = 0 | |
$customCollection | foreach { $itemCount += $_.count } | |
write-host "The total count of all items is: " -NoNewline; Write-Host "$itemCount" -ForegroundColor Green | |
<# | |
BONUS. What is the total number of cumulative gifts? | |
#> | |
[int]$cumulativeGifts = 0 | |
[int]$giftIndex = 0 | |
foreach ($day in $customCollection) | |
{ | |
$giftIndex += 1 | |
$customCollection | select -First $giftIndex | foreach { $cumulativeGifts += $_.count } | |
} | |
write-host "The total number of cumulative gifts is: " -NoNewline; Write-Host "$cumulativeGifts" -ForegroundColor Green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment