Scripting Games – December 2015

So here is my submitted entry to the December 2015 Scripting Games Puzzle hosted at PowerShell.org to which comments have been posted on the Wrap Up containing the official answer(s).

The challenges presented in this puzzle are described below and to which I detail my approach to solving each one.

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.

Firstly, I split the collection of entries and store the output as a variable. In order to sort the number by length the variable is retrieved and the objects are sorted by property length. For sorting the objects without the number, the retrieved variable replaces any numbers in the objects.

Turn each line into a custom object with a properties for Count and Item

For each object in the list of items an action is performed to create a custom object for a count and item property value. In order to retrieve property values, a regular expression is used to replace all numbers to retrieve the item property value and then to replace all alphabetic characters for the item property value.

Using your custom objects, what is the total number of all bird-related items?

Here I perform a keyword match which contains all the bird-related items agaisnt the custom object and the calculate the sum of the property value count to retrieve the total number.

What is the total count of all items?

As previously, I have used regular expression to remove all alphabetic characters from each item in the collection. I then join the returned values with the ‘+’ character and pass the string as the command parameter value for the Invoke-Expression cmdlet to perform the calculation.


# http://powershell.org/wp/2015/12/05/december-2015-scripting-games-puzzle/
$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
"@
# Challenge 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.
$Gifts = $List -Split "`n"
Write-Output "Challenge 1 – Sorting collection of entries by length.`n"
$Gifts | Sort-Object Property Length
Write-Output "`nChallenge 1 (Bonus) – Sorting collection of entries by length without the number.`n"
$Gifts -Replace "[0-9]" | Sort-Object Property Length
#Challenge 2 – Turn each line into a custom object with a properties for Count and Item.
$GiftObjects = ForEach ($Gift in $Gifts)
{
[PSCustomObject] @{
Count = $Gift -Replace '[a-zA-z]'
Item = $Gift -Replace '[0-9]'
} #PSCustomObject
} # ForEach
Write-Output "`nChallenge 2 – Create custom object for each line with count and item property."
$GiftObjects
# Challenge 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"
Write-Output ("`nChallenge 3 – The total number of bird related items: " + ($GiftObjects -match $Birds | Measure-Object Property Count Sum).Sum + "`n")
# Challenge 4 – What is the total count of all items?
Write-Output ("Challenge 4 – The total number of gift items received: " + (Invoke-Expression (($Gifts -Replace '[a-zA-Z]') -join "+")))


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s