-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21.go
More file actions
151 lines (119 loc) · 3.17 KB
/
day21.go
File metadata and controls
151 lines (119 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"os"
"panic"
"readers"
"strconv"
"strings"
)
func Day21Part1() int {
file, err := os.Open("assets/day21.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
player1Position, err := strconv.Atoi(strings.Replace(lines[0], "Player 1 starting position: ", "", 1))
panic.Check(err)
player2Position, err := strconv.Atoi(strings.Replace(lines[1], "Player 2 starting position: ", "", 1))
panic.Check(err)
var player1Points, player2Points, rolls int
d := die{value: 1, size: 100}
for true {
player1Position += d.Roll() + d.Roll() + d.Roll()
player1Position = (player1Position-1)%10 + 1
rolls += 3
player1Points += player1Position
if player1Points >= 1000 {
break
}
player2Position += d.Roll() + d.Roll() + d.Roll()
player2Position = (player2Position-1)%10 + 1
rolls += 3
player2Points += player2Position
if player2Points >= 1000 {
break
}
}
if player1Points < player2Points {
return player1Points * rolls
} else {
return player2Points * rolls
}
}
func Day21Part2() int64 {
file, err := os.Open("assets/day21.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
player1Position, err := strconv.Atoi(strings.Replace(lines[0], "Player 1 starting position: ", "", 1))
panic.Check(err)
player2Position, err := strconv.Atoi(strings.Replace(lines[1], "Player 2 starting position: ", "", 1))
panic.Check(err)
cache := make(map[state]score)
s := quantumGame(&cache, state{player1Position: player1Position, player2Position: player2Position})
if s.player1Count > s.player2Count {
return s.player1Count
} else {
return s.player2Count
}
}
func quantumGame(cache *map[state]score, s state) score {
if s.player2Score >= 21 {
return score{player2Count: 1}
}
if cachedScore, exists := (*cache)[s]; exists {
return cachedScore
}
currentScore := score{}
// We roll all possible outcomes at the same time
// see: https://old.reddit.com/r/adventofcode/comments/rl6p8y/2021_day_21_solutions/hpe4z64/
for _, distribution := range totalDieDistribution {
newPosition := (s.player1Position+distribution.dieValue-1)%10 + 1
// Swap active player
subScore := quantumGame(cache, state{
player1Score: s.player2Score,
player2Score: s.player1Score + newPosition,
player1Position: s.player2Position,
player2Position: newPosition,
})
// Swap new scores as well
currentScore.player1Count += subScore.player2Count * distribution.times
currentScore.player2Count += subScore.player1Count * distribution.times
}
(*cache)[s] = currentScore
return currentScore
}
var totalDieDistribution = []dieDistribution{
{dieValue: 3, times: 1},
{dieValue: 4, times: 3},
{dieValue: 5, times: 6},
{dieValue: 6, times: 7},
{dieValue: 7, times: 6},
{dieValue: 8, times: 3},
{dieValue: 9, times: 1},
}
type dieDistribution struct {
dieValue int
times int64
}
type state struct {
player1Score int
player2Score int
player1Position int
player2Position int
}
type score struct {
player1Count int64
player2Count int64
}
type die struct {
value int
size int
}
func (d *die) Roll() int {
next := d.value
d.value++
if d.value > d.size {
d.value = 1
}
return next
}