Skip to content

Commit

Permalink
day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Lewis committed Dec 11, 2024
1 parent 7207014 commit b34f913
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
69 changes: 69 additions & 0 deletions 11/eleven.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package eleven

import (
"strconv"
"strings"

"github.com/cdlewis/advent-of-code/util"
"github.com/cdlewis/advent-of-code/util/aoc"
"github.com/cdlewis/advent-of-code/util/cast"
)

var cache = map[[3]int]int{}

func Eleven() int {
stones := util.Map(strings.Split(aoc.GetInput(11, false, "125 17"), " "), cast.ToInt)
return simulate(stones, 75)
}

func simulate(stones []int, steps int) int {
result := 0

if len(stones) == 2 {
key := [3]int{stones[0], stones[1], steps}
if val, ok := cache[key]; ok {
return val
}
defer func() {
cache[key] = result
}()
}

for currentStep := range steps {
for idx, s := range stones {
if s == -1 {
continue
}

if s == 0 {
stones[idx] = 1
continue
}

digits := strconv.Itoa(s)
if len(digits)%2 == 0 {
stones[idx] = -1

simResult := simulate(
[]int{
cast.ToInt(digits[:len(digits)/2]),
cast.ToInt(digits[len(digits)/2:]),
},
steps-currentStep-1,
)

result += simResult
continue
}

stones[idx] *= 2024
}
}

for _, s := range stones {
if s != -1 {
result++
}
}
return result
}
9 changes: 9 additions & 0 deletions 11/eleven_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eleven

import "testing"

func TestEleven(t *testing.T) {
if Eleven() != 220357186726677 {
t.Error("unexpected result")
}
}
1 change: 1 addition & 0 deletions 11/input_11
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6563348 67 395 0 6 4425 89567 739318
4 changes: 2 additions & 2 deletions util/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (l *List) Append(i int) {
l.Head = &Node{Val: i}
l.Tail = l.Head
} else {
l.Tail = l.Tail.Append(i)
l.Tail = l.Tail.InsertAfter(i)
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ func (n *Node) ToSlice() []int {
return results
}

func (n *Node) Append(i int) *Node {
func (n *Node) InsertAfter(i int) *Node {
next := &Node{Val: i}
next.Prev = n
n.Next = next
Expand Down

0 comments on commit b34f913

Please sign in to comment.