Last active
January 16, 2018 07:35
-
-
Save shaneis/04b33a12aa1c92ebe3fce3e14eae3df1 to your computer and use it in GitHub Desktop.
Final function form!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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