advent_of_code_2024/day_11/solution.go

74 lines
1.3 KiB
Go

package day_11
import (
"strconv"
"strings"
)
type Stones map[uint64]uint64
// task:https://adventofcode.com/2024/day/11
func SolveBasic(input string) int {
stones := load(input)
for i := 0; i < 25; i++ {
stones = blink(stones)
}
sum := uint64(0)
for _, st := range stones {
sum += st
}
return int(sum)
}
// task:https://adventofcode.com/2024/day/11#part2
func SolveComplex(input string) int {
stones := load(input)
for i := 0; i < 75; i++ {
stones = blink(stones)
}
sum := uint64(0)
for _, st := range stones {
sum += st
}
return int(sum)
}
func blink(before Stones) Stones {
after := Stones{}
for stone, count := range before {
if stone == 0 {
after[1] += count
} else if val := fromUint(stone); len(val)%2 == 0 {
after[parseUint(val[:len(val)/2])] += count
after[parseUint(val[len(val)/2:])] += count
} else {
after[stone*2024] += count
}
}
return after
}
func fromUint(stone uint64) string {
return strconv.FormatUint(stone, 10)
}
func load(input string) Stones {
stones := Stones{}
for _, substring := range strings.Split(input, " ") {
stones[parseUint(substring)]++
}
return stones
}
func parseUint(substring string) uint64 {
if val, err := strconv.ParseUint(substring, 10, 64); err == nil {
return val
} else {
panic(err)
}
}