Our December 2015 puzzle comes from PowerShell.org board member Jeff Hicks, who wanted to share a little holiday fun for the season.
Instructions
The Scripting Games have been re-imagined as a monthly puzzle. We publish puzzles the first Saturday of each month, along with solutions and commentary for the previous month's puzzle. You can find them all at https://powershell.org/category/announcements/scripting-games/. Many puzzles will include optional challenges, that you can use to really push your skills.
To participate, add your solution to a public Gist (http://gist.github.com; you'll need a free GitHub account, which all PowerShellers should have anyway). After creating your public Gist, just copy the URL from your browser window and paste it, by itself, as a comment of this post. Only post one entry per person. However, remember that you can always go back and edit your Gist. We'll always pull the most recent one when we display it, so there's no need to post multiple entries if you want to make an edit.
Don't forget the main rules and purpose of these monthly puzzles, including the fact that you won't receive individual scoring or commentary on your entry.
User groups are encouraged to work together on the monthly puzzles. User group leaders should submit their group's best entry to Ed Wilson, the Scripting Guy, via e-mail, prior to the third Saturday of the month. On the last Saturday of the month, Ed will post his favorite, along with commentary and excerpts from noteworthy entries. The user group with the most "favorite" entries of the year will win a grand prize from PowerShell.org.
Our Puzzle
The 12 Days of PowerShell! It is that time of year again. Time to think about sugar plums, nutcrackers and PowerShell. Well, maybe we think about that last one all year long. Because I am a giving kind of guy, I thought I‘d give you a PowerShell present. I like to think my present is one that continues to give as it involves learning. I have a small set of challenges that shouldn’t be too difficult, should be fun and in the end educational.
In PowerShell, and I think the ISE might work best for this, create 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 "@
The variable $list is technically a single string with a length of 226. Using $list, see if you can solve these questions or challenges. I have written these in such a way that the solutions build on earlier answers.
- 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.
- Turn each line into a custom object with a properties for Count and Item.
- Using your custom objects, what is the total number of all bird-related items?
- What is the total count of all items?
For those of you who have been extra good this year, I have a bonus challenge (or maybe you’ll think it is a lump of coal). Some people interpret The 12 Days of Christmas cumulatively. That is, on day 1 your true love got 1 item. On the second day, your true love got 2 turtle doves AND a partridge in a pair tree. This is in addition to the previous day’s presents. If you were to manually plot this in PowerShell you might do:
$t = 0 $t += 1 $t += 1+2 $t += 1+2+3
…
But you should be more elegant. Using PowerShell what is the total number of cumulative gifts?
https://gist.github.com/dfrankel33/e4e62e08dd07a954ff33
https://gist.github.com/dfrankel33/b946525baabf8c3d91cf#file-dec15_scriptinggames-ps1
https://gist.github.com/SadProcessor/48e1049cc50383b34960
https://gist.github.com/sqlchow/cdc16aa9087eed0d5608
https://gist.github.com/adbertram/3b7622f7ffa47fba17ba
https://gist.github.com/Windos/763f02af035e13286d29
https://gist.github.com/brianbunke/ea95576fdc978b2bc6bf
https://gist.github.com/MVKozlov/8a479d524c7fb5fa767f
https://gist.github.com/QCesarJr/fe43308dce4e156f0ea7
https://gist.github.com/brianbenson/35302df292ee07f4daa7
Merry Christmas all!
https://gist.github.com/bundyfx/a961b8a2d09a97549fe0
https://gist.github.com/IISResetMe/25d7b8191f7bd2fb3349
https://gist.github.com/kvprasoon/4dfaca07bda7bc1ffd81
https://gist.github.com/mattmcnabb/762090b1e4b566363113
https://gist.github.com/mmarchese/a7b7428d785b48d85cb2
Fun ones. Interesting math facts on the bonus challenge:
To the question: how many gifts did I get on day N? The problem is interpreted geometrically as the number of balls in a triangle with length N. Think about placing one ball, then 2, then 3, and so on raising the side length of an equilaterial triangle for each day. The closed form is n(n+2)/2 balls, and a powershell function:
To the question: how many gifts did I get from day 1 until N, this problem is interpreted geometrically as the number of balls in a triangular pyramid in which each edge contains n balls. Visualize similar to before, but you add an entire triangle instead of just an edge. The closed form of this summation is n(n+1)(n+2)/6, or this powershell function:
These sequences are described at the Online Encyclopedia of Integer Sequences as the Triangular and Tetrahedral (or Pyramidal) Number Summations, respectively with binomial coefficients C(n+1,2) and C(n+2,3).
Edit: The triangular sum is actually n(n+1)/2. Whoops!
https://gist.github.com/jbuenting/cfab1272ece134f8283f
https://gist.github.com/dean1609/533e7ff92fb5e4572be1
Decided to do it a dirty way:
https://gist.github.com/shwnstrmn/dac71d05f81bfa5aaf47
https://gist.github.com/KirillPashkov/9374331ce395466ede81
https://gist.github.com/sellynx/b82404d77879dc7a5b8d
https://gist.github.com/githubbery/d2ba4b0252a53bb000ce
https://gist.github.com/pandiculator/87ff9407716f55eb2460#file-sg_puzzle_1215-ps1
https://gist.github.com/Keithlr2/a068951bf09f84bbffff
https://gist.github.com/BennettBenson/fc4b73be4f0403c89df6
[…] 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 […]