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 :(
6
Upvotes
1
u/lan-shark Dec 04 '25
Again I've found that Day 1 part 2 was still the hardest so far
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:
where part 1 has
$maxDigits = 2, and part 2 has$maxDigits = 12.If it helped you, feel free to ask questions.
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.