Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#region ListDefinition
$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
"@ -split '\n'
#endregion
#region ObjectCreation
$List = $List | ForEach-Object {
$_ -match '^(\d+?)\s(.*)' | Out-Null
[pscustomobject] @{
'OriginalName' = $_
'GiftName' = $Matches[2]
'GiftCount' = $Matches[1]
'BirdGift' = if ($_ -match 'Partridge|Dove|Hen|Bird|Geese|Swan') {$true} else {$false}}
}
#endregion
#region CumulativeLoop
for (($i = 0),($CumulativeGifts = 0),($CumulativeBirds = 0),([array]$AccumulatedGifts = $null); $i -lt $List.Length; $i++) {
$CumulativeGiftCalculation = ($List[$i..0] | Measure-Object -Property GiftCount -Sum).Sum
$CumulativeGifts += $CumulativeGiftCalculation
$AccumulatedGifts += [pscustomobject] @{
'Day' = "Day $($i+1)"
'Daily Gift Total' = $CumulativeGiftCalculation
'Total Gifts To Date' = $CumulativeGifts
}
#Additional Bonus. See "Lessons Learned".
$CumulativeBirds += ($List[$i..0] | Where-Object -FilterScript {$_.BirdGift -eq $true} | Measure-Object -Property GiftCount -Sum).Sum
}
#endregion
#region Data-Output
$12Trees = ( [char]::ConvertFromUtf32(0x0001F384) + " " ) * 12
$Separator = "`n" + $12Trees + "`n"
#Write-Host is almost always EVIL, I know. But not here.
cls
Write-Host $Separator -ForegroundColor Green
Write-Host "Challenge 1:`n" -ForegroundColor Red
Write-Host "Here are the gifts, in order from longest to shortest, WITH their numbers:`n" -ForegroundColor Yellow
Write-Host -Object $($List.OriginalName | Sort-Object -Property Length -Descending) -Separator "`n"
Write-Host $Separator -ForegroundColor Green
Write-Host "Challenge 1.Bonus:`n" -ForegroundColor Red
Write-Host "Here are the gifts, in order from longest to shortest, WITHOUT their numbers:`n" -ForegroundColor Yellow
Write-Host -Object ($List.GiftName | Sort-Object -Property Length -Descending) -Separator "`n"
Write-Host $Separator -ForegroundColor Green
Write-Host "Challenge 2:`n" -ForegroundColor Red
Write-Host "Here are the created objects with their respective properties:`n" -ForegroundColor Yellow
Write-Output $List | Format-Table -Wrap
Write-Host $Separator -ForegroundColor Green
Write-Host "Challenge 3:`n" -ForegroundColor Red
Write-Host "$(($List.BirdGift -eq $true | Measure).count) gifts are bird-related." -ForegroundColor Yellow
Write-Host $Separator -ForegroundColor Green
Write-Host "Challenge 4:`n" -ForegroundColor Red
Write-Host "The total count of all gifts is $(($List.GiftCount | Measure -Sum).Sum)." -ForegroundColor Yellow
Write-Host $Separator -ForegroundColor Green
Write-Host "Naughty or Nice Bonus:`n" -ForegroundColor Red
Write-Host "Here's a daily chart of cumulative gifts.`n" -ForegroundColor Yellow
Write-Output $AccumulatedGifts | Format-Table -AutoSize
Write-Host $Separator -ForegroundColor Green
Write-Host "Lessons Learned:`n" -ForegroundColor Red
Write-Host "One would think that more gifts are better, but, cumulatively, that's A LOT of birds. $($CumulativeBirds) birds, to be exact." -ForegroundColor Yellow
Write-Host $Separator -ForegroundColor Green
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment