Skip to content

Instantly share code, notes, and snippets.

@Windos
Last active December 7, 2015 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Windos/763f02af035e13286d29 to your computer and use it in GitHub Desktop.
Save Windos/763f02af035e13286d29 to your computer and use it in GitHub Desktop.
December 2015 Scripting Games Submission, Windos
$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 $list into a collection of entries, as you typed them, and sort the results by length.'
$list.Split("`n") | Sort-Object -Property Length
# As a bonus, see if you can sort the length without the number.
$list.Split("`n") -replace '\d+ ' | Sort-Object -Property Length
# Turn each line into a custom object with a properties for Count and Item.
$list.Split("`n") | ForEach-Object { New-Object -TypeName PSCustomObject -Property @{'Count' = $_.Split(' ')[0]; 'Item' = ($_ -replace '\d+ ')} }
# Using your custom objects, what is the total number of all bird-related items?
# n.b. Have assumed the request was for the sum of bird related items, and not the count of lines referencing birds.
$gifts = $list.Split("`n") | ForEach-Object { New-Object -TypeName PSCustomObject -Property @{'Count' = $_.Split(' ')[0]; 'Item' = ($_ -replace '\d+ ')} }
($gifts | Where-Object -FilterScript {$_.Item -match 'Partridge|Doves|Hens|Birds|Geese|Swans'} | Measure-Object -Sum -Property Count).Sum
# What is the total count of all items?
# n.b. Using PSCustomObjects from previous challenge.
($gifts | Measure-Object -Sum -Property Count).Sum
# Using PowerShell what is the total number of cumulative gifts?
# Recursion! (using PSCustomObjects from previous challenge.
function CountGifts {
param (
[int] $sum = 0,
[int] $index = 0,
[PSCustomObject[]] $collection
)
if ($index -eq $collection.Count ) {
$sum
}
else {
for ($i = $index; $i -gt -1; $i--) {
$sum += $collection[$i].Count
}
CountGifts -sum $sum -index ($index + 1) -collection $collection
}
}
CountGifts -collection $gifts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment