2015-December Scripting Games Wrap-Up

Happy New Year! Here's the official answer to our December 2015 Games, where PowerShell.org Board Member challenged you to add some code to your holiday spirit. Want to share your own puzzle for a future Games? Drop a line to admin@ via email!

Official Answer

You started with this here string.

 

$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.image001

 

2. As a bonus, see if you can sort the length without the number.image002

 

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

$gifts = $list -split "`n" | foreach {
$split =  $_ -split " ",2
[pscustomobject]@{Count=$split[0];Item = $split[1].trim()}
}

image003

 

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

$gifts | where {$_.item -match "partridge|doves|hens|birds|geese|swans"} |
measure-object -Property count -sum | Select Sum

image004

 

5. What is the total count of all items?

$gifts | measure-object -Property count -sum | select Sum

image005

Bonus:

What is the cumulative or literal count? I used an algorithm I found at http://edspi31415.blogspot.co.uk/2011/12/12-days-of-christmas.html that I turned into a PowerShell expression:

$daily = 0
$total = 0
for ($i = 1 ;$i -le 12 ; $i++) {
#get the daily total for each day cumulatively
$daily+=$i
#add the daily total to the grand total
$total+=$daily
}

image006

 

Hope you had fun! Happy Holidays and hoping you have a healthy and prosperous PowerShell New Year.

 

Posted in:
About the Author

PowerShell.org Announcer

This is the official account for PowerShell.org and sponsor announcements.