Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2015-12-ScriptingGamesPuzzle
<#
.SYNOPSIS
Scripting games puzzle for December 2015
.DESCRIPTION
ALL REQUIREMENTS
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.
2. Turn each line into a custom object with a properties for Count and Item.
3. Using your custom objects, what is the total number of all bird-related items?
4. What is the total count of all items?
5. Using PowerShell what is the total number of cumulative gifts?
#>
Function Read-Lyric {
[CmdletBinding()]
param(
[parameter(Mandatory=$True)]
[string]$list
)
$Count = $null; $Item = $null ; $12DaysofXmas = $null ; $Output = $Null
$Gifts = @{
"Total" = 0
"Cumulative" = 0
"XmasCol" = $null
}
$Gifts.XmasCol = @{}
$Birds = @{
"Types" = @("Partridge","Doves","Hens","Birds","Geese","Swans")
"Count" = 0
}
Function Get-TotalCount {
# Formula for Total of series of numbers is n(n+1)/2 where n is the largest number in the series
# from http://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/
param(
[parameter(Mandatory=$True)]
[int]$n
)
$TotalCount = ($n * ($n + 1))/2
Return $TotalCount
}
Function Get-CumulativeTotal {
# Formula for cumulative number of Gifts is n(n+1)(n+2)/6 where n is the largest number in the series
# from http://www.billthelizard.com/2011/12/n-days-of-christmas-solution.html
param(
[parameter(Mandatory=$True)]
[int]$n
)
$CumulativeTotal = (($n * ($n + 1) * ($n + 2))/6)
Return $CumulativeTotal
}
# 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.
$Output = "1. Splitting list and sorting by length from shortest to longest with bonus of no number: " + "`n"
Write-Verbose -Message $Output
$12DaysofXmas = ($list -split ("`n")).Trim() | Sort-Object -Property Length
($12DaysofXmas -split "\d+").trim()|where-object {$_ -ne ""}
# 2. Turn each line into a custom object with properties for Count and Item.
$Output= "2. Creating a custom object with properties for Count and Item: " + "`n"
Write-Verbose -Message $Output
$Gifts.XmasCol = $12DaysofXmas | Foreach-Object {
$Count, $Item = ($_ -split "\s", 2).trim()
[PSCustomObject]@{ "Count" = [int]$Count ; "Item" = [string]$Item }
}
$Gifts.XmasCol|format-table -auto
# 3. Using your custom objects, what is the total number of all bird-related items?
$Output= "3. Using the custom object to find the total number of all bird-related items: " + "`n"
Write-Verbose -Message $Output
$Gifts.XmasCol|foreach-Object { Foreach($Bird in $Birds.Types){ IF ($_.Item -Match $Bird) {$Birds.Count++}}}
$Output= "Total number of bird-related items: " + $Birds.Count
Write-Output -InputObject $Output
$MaxNuminSeries = [int]($Gifts.XmasCol|Measure-object -Maximum).Count
# 4. What is the total count of all items?
$Output = "4. Finding the total count of all items: " + "`n"
Write-Verbose -Message $Output
$Gifts.Total = Get-TotalCount $MaxNuminSeries
$Output = "Total number of number of gifts: " + $Gifts.Total
Write-Output -InputObject $Output
# 5. Using PowerShell what is the total number of cumulative gifts?
$Output = "5. Finding the cumulative total of all items: " + "`n"
Write-Verbose -Message $Output
$Gifts.Cumulative = Get-CumulativeTotal $MaxNuminSeries
$Output = "Total Cumulative gifts: " + $Gifts.Cumulative
Write-Output -InputObject $Output
}
$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
"@
Read-Lyric $list -verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment