58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
|
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)
|
||
|
}
|
||
|
}
|