From e0f1c3d9bcef754a7384c06d55e8ae8a244c39a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borna=20Rajkovi=C4=87?= Date: Wed, 11 Dec 2024 10:17:51 +0100 Subject: [PATCH] Day 11. part 2. --- day_11/solution.go | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/day_11/solution.go b/day_11/solution.go index f4b7b65..db9a27a 100644 --- a/day_11/solution.go +++ b/day_11/solution.go @@ -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) + } + + sum := uint64(0) + for _, st := range stones { + sum += st + } + return int(sum) } -func blink(before []uint64) []uint64 { - after := make([]uint64, 0, len(before)) - for _, stone := range before { +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 }