advent_of_code_2024/day_11/solution.go

58 lines
1.1 KiB
Go
Raw Normal View History

2024-12-11 09:04:55 +00:00
package day_11
import (
"strconv"
"strings"
)
// task:https://adventofcode.com/2024/day/11
func SolveBasic(input string) int {
stones := load(input)
for i := 0; i < 25; i++ {
stones = blink(stones)
}
return len(stones)
}
// task:https://adventofcode.com/2024/day/10#part2
func SolveComplex(input string) int {
return 0
}
func blink(before []uint64) []uint64 {
after := make([]uint64, 0, len(before))
for _, stone := range before {
if stone == 0 {
after = append(after, 1)
} else if val := fromUint(stone); len(val)%2 == 0 {
after = append(after, parseUint(val[:len(val)/2]))
after = append(after, parseUint(val[len(val)/2:]))
} else {
after = append(after, stone*2024)
}
}
return after
}
func fromUint(stone uint64) string {
return strconv.FormatUint(stone, 10)
}
func load(input string) []uint64 {
stones := []uint64{}
for _, substring := range strings.Split(input, " ") {
stones = append(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)
}
}