}
type gameState struct {
p1 player
p2 player
}
var cache = map[gameState][2]int{}
func solveSecond() int {
p1 := player{p1s - 1, 0}
p2 := player{p2s - 1, 0}
res := countWin(gameState{p1, p2})
if res[0] > res[1] {
return res[0]
}
return res[1]
}
// https://github.com/jonathanpaulson/AdventOfCode/blob/master/2021/21.py
// This one is the go version on Jonathan's code. I am speechless how elegant this
// solution is, and amazed by how fast he has written it.
func countWin(s gameState) [2]int {
if s.p1.score >= maxScore2 {
return [2]int{1, 0}
}
if s.p2.score >= maxScore2 {
return [2]int{0, 1}
}
if ret, ok := cache[s]; ok {
return ret
}
var ret [2]int
for _, d1 := range []int{1, 2, 3} {
for _, d2 := range []int{1, 2, 3} {
for _, d3 := range []int{1, 2, 3} {
nextPos := (s.p1.pos + d1 + d2 + d3) % 10
nextScore := s.p1.score + nextPos + 1
p2Turn := countWin(gameState{s.p2, player{nextPos, nextScore}})
ret = [2]int{ret[0] + p2Turn[1], ret[1] + p2Turn[0]}
}
}
}
cache[s] = ret
return ret