Files
adventofcode/2025/gareth/day03/day03.go
Gareth 7f6c31c003 Day03
2025-12-03 20:03:14 +00:00

70 lines
1.2 KiB
Go

package day03
import (
"strconv"
"strings"
)
func Part1(input string) int {
lines := strings.Split(input, "\n")
output := 0
for _, bank := range lines {
best := 0
length := len(bank)
for i := 0; i < length; i++ {
tens, _ := strconv.Atoi(string(bank[i]))
for j := i + 1; j < length; j++ {
ones, _ := strconv.Atoi(string(bank[j]))
value := tens*10 + ones
if value > best {
best = value
}
}
}
output += best
}
return output
}
func Part2(input string) int {
lines := strings.Split(strings.TrimSpace(input), "\n")
total := 0
k := 12
for _, bank := range lines {
bank = strings.TrimSpace(bank)
n := len(bank)
start := 0
chosen := make([]byte, 0, k)
for picked := 0; picked < k; picked++ {
// leave enough digits to complete the subsequence
last := n - (k - picked)
bestChar := bank[start]
bestIdx := start
for i := start + 1; i <= last; i++ {
if bank[i] > bestChar {
bestChar = bank[i]
bestIdx = i
if bestChar == '9' {
break
}
}
}
chosen = append(chosen, bestChar)
start = bestIdx + 1
}
val, _ := strconv.Atoi(string(chosen))
total += val
}
return total
}