Day 11. part 2.

This commit is contained in:
Borna Rajković 2024-12-11 10:17:51 +01:00
parent e8e29e2701
commit e0f1c3d9bc
1 changed files with 29 additions and 13 deletions

View File

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