-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
294 lines (274 loc) · 8.15 KB
/
main.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"fmt"
_ "sync"
"time"
)
func main() {
myInput := [2]int{4, 1}
example := [2]int{4, 8}
fmt.Println(myInput)
fmt.Println(example)
lines := myInput
start := time.Now()
partA(lines)
duration := time.Since(start)
partB(lines)
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
var determinisitcDie int = 1
func getDeterNDiesRollResult(n int) int {
sum := 0
for n > 0 {
if determinisitcDie == 101 {
determinisitcDie = 1
}
sum += determinisitcDie
determinisitcDie++
n--
}
if sum > 101+100+99 {
panic("something wrong with the dice")
}
return sum
}
func newPos(oldPos, diceResult int) int {
movement := diceResult % 10
newPos := oldPos + movement
for newPos >= 11 {
newPos -= 10
}
return newPos
}
func partA(lines [2]int) {
p1score := 0
p2score := 0
p1pos := lines[0]
p2pos := lines[1]
diceRolls := 0
result := 0
for i := 0; ; i++ {
// Player 1 rolls 1+2+3 and moves to space 10 for a total score of 10.
diceResult := getDeterNDiesRollResult(3)
p1pos = newPos(p1pos, diceResult)
if p1pos < 0 || p1pos > 10 {
panic("player1pos error")
}
p1score += p1pos
diceRolls += 3
if p1score >= 1000 {
fmt.Printf("Player 1 rolls %d and moves to space %d for a total score of %d.\n", diceResult, p1pos, p1score)
fmt.Printf("Game ends: Dice Rolls: %d, P1 Score: %d P2 Score: %d\n", diceRolls, p1score, p2score)
result = diceRolls * p2score
break
}
diceResult = getDeterNDiesRollResult(3)
p2pos = newPos(p2pos, diceResult)
if p2pos < 0 || p2pos > 10 {
println(p2pos)
panic("player2pos error")
}
p2score += p2pos
diceRolls += 3
if p2score >= 1000 {
fmt.Printf("Player 2 rolls %d and moves to space %d for a total score of %d.\n", diceResult, p2pos, p2score)
fmt.Printf("Game ends: Dice Rolls: %d, P1 Score: %d P2 Score: %d\n", diceRolls, p1score, p2score)
result = diceRolls * p1score
break
}
}
fmt.Printf("Solution for part A: %v\n", result)
}
var universesPerDiceResult = map[int]uint64{
3: 1,
4: 3,
5: 6,
6: 7,
7: 6,
8: 3,
9: 1}
type universe struct {
p1score, p2score, p1pos, p2pos, totalscore int
}
func partB(lines [2]int) {
limit := 1000
start1, start2 := lines[0], lines[1]
start := time.Now()
p1WinsA, p2WinsA := simulateAllrounds(start1, start2, limit)
duration := time.Since(start)
//p1WinsB, p2WinsB := checkAllUniverses(start1, start2, limit)
duration2 := time.Since(start)
result := p1WinsA
if p1WinsA < p2WinsA {
result = p2WinsA
}
fmt.Printf("allRounds: %s, allUniverses: %s\n", duration, duration2-duration)
fmt.Printf("Solution for part B: %v\n", result)
}
func bruteForce(start1, start2, limit int) (uint64, uint64) {
var p1Wins uint64 = 0
var p2Wins uint64 = 0
universeCounter := map[universe]uint64{}
startUniverse := universe{0, 0, start1, start2, 0}
universeCounter[startUniverse] = 1
for len(universeCounter) > 0 {
for uni, counter := range universeCounter {
// handle first player
for diceResult, addedUnis1 := range universesPerDiceResult {
pos1 := newPos(uni.p1pos, diceResult)
p1Score := uni.p1score + pos1
// check if plaxer 1 has reached the limit
if p1Score >= limit {
delete(universeCounter, uni)
p1Wins += counter + addedUnis1
continue
}
for diceResult, addedUnis2 := range universesPerDiceResult {
pos2 := newPos(uni.p2pos, diceResult)
p2score := uni.p2score + pos2
// check if plaxer 2 has reached the limit
if p2score >= limit {
delete(universeCounter, uni)
p2Wins += counter + addedUnis1*addedUnis2
continue
}
// no win yet
newUniverse := universe{p1score: p1Score, p2score: p2score, p1pos: pos1, p2pos: pos2}
universeCounter[newUniverse] += uint64(addedUnis1 * addedUnis2)
}
}
}
}
return p1Wins, p2Wins
}
func checkAllUniverses(start1, start2, limit int) (uint64, uint64) {
var p1Wins uint64 = 0
var p2Wins uint64 = 0
// initialize multiverse
universeCounter := map[universe]uint64{}
startUniverse := universe{0, 0, start1, start2, 0}
universeCounter[startUniverse] = 1
// check universe with ascending totalscore.
// that way "lower" universes are checked first and will not be visited again
for totalscore := 0; totalscore < 2*limit-1; totalscore++ {
// check all possible scores for p1
for p1score := 0; p1score < limit; p1score++ {
// scores for p2 can be calculated from previous loops
p2score := totalscore - p1score
if p1score > 20 {
continue
}
// check all possible p1 positions
for p1pos := 1; p1pos < 11; p1pos++ {
// check all possible p2 positions
for p2pos := 1; p2pos < 11; p2pos++ {
// create matching universe
currentUni := universe{
totalscore: totalscore,
p1score: p1score,
p2score: p2score,
p1pos: p1pos,
p2pos: p2pos}
counter := universeCounter[currentUni]
// no further need for the universe:
delete(universeCounter, currentUni)
// universe can't be reached if it has no visits yet
if counter == 0 {
continue
}
// check if player 1 can win
for diceResult, addedUnis1 := range universesPerDiceResult {
newP1pos := newPos(p1pos, diceResult)
newP1Score := p1score + newP1pos
if newP1Score >= limit {
// add the universes from the start multiplied by the ones with this future
p1Wins += counter * addedUnis1
// check next player 1 dice throw possibility
continue
}
// check if player 2 can win
for diceResult, addedUnis2 := range universesPerDiceResult {
newP2pos := newPos(p2pos, diceResult)
newP2Score := p2score + newP2pos
if newP2Score >= limit {
p2Wins += counter * addedUnis1 * addedUnis2
// check another throw of player 2
continue
}
// no player has won
nextUni := universe{
totalscore: newP1Score + newP2Score,
p1score: newP1Score,
p2score: newP2Score,
p1pos: newP1pos,
p2pos: newP2pos}
// write result to the universeCounter for later calcualtions
universeCounter[nextUni] += counter * addedUnis1 * addedUnis2
//checked all player 2 throw possibilites
}
//checked all player 1 throw possibilites
}
// checked all p2 positions
}
//checked all p1 positions
}
//checked all p1 scores
}
//checked all totalscores
}
return p1Wins, p2Wins
}
func simulateAllrounds(start1, start2, limit int) (uint64, uint64) {
var p1Wins uint64 = 0
var p2Wins uint64 = 0
// initialize multiverse
universeCounter := map[universe]uint64{}
startUniverse := universe{
totalscore: 0,
p1score: 0,
p2score: 0,
p1pos: start1,
p2pos: start2}
universeCounter[startUniverse] = 1
activeUniverses := 1
for activeUniverses > 0 {
//fmt.Printf("Participating Universes: %d\n", len(universeCounter))
activeUniverses = 0
newUniverseCounter := map[universe]uint64{}
for uni, counter := range universeCounter {
if counter == 0 {
continue
}
activeUniverses++
for diceResult, multi1 := range universesPerDiceResult {
newP1pos := newPos(uni.p1pos, diceResult)
newP1Score := uni.p1score + newP1pos
if newP1Score >= limit {
p1Wins += counter * multi1
universeCounter[uni] = 0
continue
}
for diceResult, multi2 := range universesPerDiceResult {
newP2pos := newPos(uni.p2pos, diceResult)
newP2Score := uni.p2score + newP2pos
if newP2Score >= limit {
p2Wins += counter * multi1 * multi2
universeCounter[uni] = 0
continue
}
nextUniverse := universe{
totalscore: newP1Score + newP2Score,
p1score: newP1Score,
p2score: newP2Score,
p1pos: newP1pos,
p2pos: newP2pos}
universeCounter[uni] = 0
newUniverseCounter[nextUniverse] += counter * multi1 * multi2
}
}
}
universeCounter = newUniverseCounter
}
return p1Wins, p2Wins
}