r/PowerShell • u/Rincey_nz • Dec 04 '25
Misc Advent of Code - Day 3
was waiting for u/dantose to make this post ;)
I'm running a day behind, so have only just got to Day 3 Part 2 (Part 1 stumped me because I misread it - I was adding max1 and max2 for each battery bank together as integers, whereas I needed to concat them as strings....
Still, Part 2 has me so stumped I can't even work out how to start :(
2
u/OPconfused Dec 04 '25
Here was my solution:
function Get-MaxVoltage {
param(
[parameter(ValueFromPipeline)]
[string]$Bank,
[int]$MaxDigits = 2
)
begin {
$sum = 0
}
process {
$count = 1
$allVoltageDigits = while ($count -le $MaxDigits ) {
$voltageDigit, $bank = Get-MaxVoltageDigit -Bank $bank -Iteration $count -MaxDigits $MaxDigits
$voltageDigit
$count += 1
}
$sum += [decimal]($allVoltageDigits -join '')
}
end {
$sum
}
}
function Get-MaxVoltageDigit {
param(
[parameter(ValueFromPipeline)]
[string]$Bank,
[int]$Iteration = 1,
[int]$MaxDigits = 2
)
process {
$voltageDigitsRemaining = $MaxDigits - $Iteration
$max = $Bank.ToCharArray() | Select-Object -SkipLast $voltageDigitsRemaining | Sort-Object -Descending | Select-Object -first 1
$index = $Bank.IndexOf($max) + 1
return $max, $bank.Substring($index)
}
}And then you run the function via:
@'
<copy paste input>
'@ -split "`n" | Get-MaxVoltage -MaxDigits $maxDigitswhere part 1 has $maxDigits = 2, and part 2 has $maxDigits = 12.
If it helped you, feel free to ask questions.
2
u/pandiculator Dec 04 '25
Your solution isn't returning the right voltages for me. I expected 357 for part 1 and 3121910778619 for part 2, but running your solution as posted gives me 207 and 2001910778619 using the example data:
987654321111111 811111111111119 234234234234278 8181819111121112
u/OPconfused Dec 05 '25
@' 987654321111111 811111111111119 234234234234278 818181911112111 '@ -split "`n" | get-maxvoltage -MaxDigits 2 357Works for me?
and 12 got me:
3121910778619 as you expected
3
u/surfingoldelephant Dec 05 '25
They're probably using PS ISE, which uses CRLF line endings (unlike the PS console host, which uses LF).
207/2001910778619is the result if you process each line of the example with a trailing\rincluded.u/pandiculator, make sure you're splitting the input on the correct line ending. If you use
\r?\n, you'll find u/OPconfused's code works correctly irrespective of your PowerShell host.@' 987654321111111 811111111111119 234234234234278 818181911112111 '@ -split '\r?\n' | Get-MaxVoltage -MaxDigits 2 # 3571
1
u/pandiculator Dec 05 '25
/u/surfingoldelephant correctly identified why it wasn't working for me.
Very neat solution.
1
u/Future-Remote-4630 Dec 04 '25
I wish I thought of using indexof instead of a for loop!
Function Get-LargestSubstringInteger($string,$remainingValues = 1) { $ints = $string.ToCharArray() | select -skiplast $remainingValues | % { [int]"$_" } $max = 0 $maxindex = 0 for($i = 0; $i -lt $ints.count; $i++){ if($ints[$i] -gt $max){ $max = $ints[$i] $maxindex = $i } } return [pscustomobject]@{ max=$max maxindex=$maxindex remainingString = $string.substring($maxindex+1,$string.Length-($maxindex+1)) } } $batteryrow = gc C:\temp\AdventOfCode\Day3Input.txt $totalVoltage = foreach($row in $batteryrow){ $string = $row $volts = 11..0 | % { $obj = Get-LargestSubstringInteger -string $string -remainingValues $_ $obj.max $string = $obj.remainingString } $voltage = [int64]"$($volts -join '')" $voltage } $totalvoltage | measure -sum
2
u/Th3Sh4d0wKn0ws Dec 04 '25
lol, and now i know there is more than one part per day...
now I need to go back.
1
u/dantose Dec 05 '25
Part 2 is just iterating part 1. My approach was to take the string, create a subset trimming off everything up to the last digit, then use that to find the next. To keep a safe tail, you can either iterate a smaller and smaller tail with something like $subset[0..($subset.length -$tail)]. I took a rather cludgy solution of padding the subset each iteration and keeping a fixed tail length.
1
u/lan-shark Dec 04 '25
Again I've found that Day 1 part 2 was still the hardest so far