-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Lewis
committed
Dec 11, 2024
1 parent
7207014
commit b34f913
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
6563348 67 395 0 6 4425 89567 739318 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters