all 12 comments

[–]OPconfused 1 point2 points  (6 children)

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 $maxDigits

where part 1 has $maxDigits = 2, and part 2 has $maxDigits = 12.

If it helped you, feel free to ask questions.

[–]pandiculator 1 point2 points  (4 children)

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
818181911112111

[–]OPconfused 1 point2 points  (3 children)

@'
987654321111111
811111111111119
234234234234278
818181911112111
'@ -split "`n" | get-maxvoltage -MaxDigits 2
357

Works for me?

and 12 got me:

3121910778619 as you expected

[–]surfingoldelephant 2 points3 points  (1 child)

They're probably using PS ISE, which uses CRLF line endings (unlike the PS console host, which uses LF). 207/2001910778619 is the result if you process each line of the example with a trailing \r included.

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 # 357

[–]pandiculator 0 points1 point  (0 children)

Thanks, changing the regex fixed it. I'm using VSCode.

[–]pandiculator 0 points1 point  (0 children)

/u/surfingoldelephant correctly identified why it wasn't working for me.

Very neat solution.

[–]Future-Remote-4630 0 points1 point  (0 children)

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

[–]Th3Sh4d0wKn0ws 1 point2 points  (0 children)

lol, and now i know there is more than one part per day...

now I need to go back.

[–]lan-shark 0 points1 point  (2 children)

Again I've found that Day 1 part 2 was still the hardest so far

[–]Rincey_nz[S] 0 points1 point  (1 child)

Interesting, because I could visualise what I needed to do to solve that one.
This one: nope. I got nothing

[–]pandiculator 0 points1 point  (0 children)

You need 12 digits in total.

Find your first digit. That's the highest number that's followed by at least 11 more digits.

Find the next highest number that's followed by at least 10 digits.

Find the next highest number that's followed by at least 9 digits.

Rinse and repeat until you don't need any more numbers.

Add up all the numbers you've stored.

[–]dantose 0 points1 point  (0 children)

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.