Day 11. part 2.
This commit is contained in:
parent
e8e29e2701
commit
e0f1c3d9bc
|
@ -5,6 +5,8 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type Stones map[uint64]uint64
|
||||
|
||||
// task:https://adventofcode.com/2024/day/11
|
||||
func SolveBasic(input string) int {
|
||||
stones := load(input)
|
||||
|
@ -13,24 +15,38 @@ func SolveBasic(input string) int {
|
|||
stones = blink(stones)
|
||||
}
|
||||
|
||||
return len(stones)
|
||||
sum := uint64(0)
|
||||
for _, st := range stones {
|
||||
sum += st
|
||||
}
|
||||
return int(sum)
|
||||
}
|
||||
|
||||
// task:https://adventofcode.com/2024/day/10#part2
|
||||
// task:https://adventofcode.com/2024/day/11#part2
|
||||
func SolveComplex(input string) int {
|
||||
return 0
|
||||
stones := load(input)
|
||||
|
||||
for i := 0; i < 75; i++ {
|
||||
stones = blink(stones)
|
||||
}
|
||||
|
||||
func blink(before []uint64) []uint64 {
|
||||
after := make([]uint64, 0, len(before))
|
||||
for _, stone := range before {
|
||||
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 = append(after, 1)
|
||||
after[1] += count
|
||||
} 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:]))
|
||||
after[parseUint(val[:len(val)/2])] += count
|
||||
after[parseUint(val[len(val)/2:])] += count
|
||||
} else {
|
||||
after = append(after, stone*2024)
|
||||
after[stone*2024] += count
|
||||
}
|
||||
}
|
||||
return after
|
||||
|
@ -40,10 +56,10 @@ func fromUint(stone uint64) string {
|
|||
return strconv.FormatUint(stone, 10)
|
||||
}
|
||||
|
||||
func load(input string) []uint64 {
|
||||
stones := []uint64{}
|
||||
func load(input string) Stones {
|
||||
stones := Stones{}
|
||||
for _, substring := range strings.Split(input, " ") {
|
||||
stones = append(stones, parseUint(substring))
|
||||
stones[parseUint(substring)]++
|
||||
}
|
||||
return stones
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue