Skip to content

Instantly share code, notes, and snippets.

@shaneis
Last active January 16, 2018 07:35
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 shaneis/04b33a12aa1c92ebe3fce3e14eae3df1 to your computer and use it in GitHub Desktop.
Save shaneis/04b33a12aa1c92ebe3fce3e14eae3df1 to your computer and use it in GitHub Desktop.
Final function form!
function Get-AOCDay01Part01 {
[CmdletBinding()]
param (
[string]$InputObject
)
begin {
#Initialize a holding variable
[int]$Sum = $null
#Split the input
$Digits = (($PuzzleNumber -split '') -ne '')
# First number
[int]$StartNumber = $Digits[0]
# Last number (yeah, -1 means the last!)
[int]$EndNumber = $Digits[-1]
}
process {
$Digits | ForEach-Object {
$CurrentNumber = $PSItem
# It they match, we add them
if ($CurrentNumber -eq $PreviousNumber)
{
$Sum += $CurrentNumber
}
$PreviousNumber = $CurrentNumber
}
}
end {
# Finally, check if we add the first number if it matches the last ("The list is circular")
if ($StartNumber -eq $EndNumber)
{
[int]$Sum += $StartNumber
}
$Sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment