-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.go
186 lines (161 loc) · 3.72 KB
/
day21.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/dergeberl/aoc/utils"
)
type player struct {
position int
score int
}
type game struct {
player1 player
player2 player
currentPlayer int
}
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay21Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay21Part2(string(input)))
}
//SolveDay21Part1 returns the number of rolled die multiplied by loosing player score
//This game is played the normal way
func SolveDay21Part1(input string) int {
g := parseInput(input)
var curNum, cur3Nums, dieCount int
for {
dieCount += 3
cur3Nums, curNum = getNext3DiceRoles(curNum)
if g.currentPlayer == 1 {
g.player1.position += cur3Nums
for g.player1.position > 10 {
g.player1.position -= 10
}
g.player1.score += g.player1.position
if g.player1.score >= 1000 {
return (dieCount) * g.player2.score
}
g.currentPlayer = 2
continue
}
g.player2.position += cur3Nums
for g.player2.position > 10 {
g.player2.position -= 10
}
g.player2.score += g.player2.position
if g.player2.score >= 1000 {
return (dieCount) * g.player1.score
}
g.currentPlayer = 1
}
}
//SolveDay21Part2 returns the number of wins of the better performing input after play a strange universe round
func SolveDay21Part2(input string) int64 {
g := parseInput(input)
p1, p2 := g.playUniverseMode(nil)
if p1 > p2 {
return p1
}
return p2
}
//playUniverseMode returns the number of wins for player1 and player2 after all possible games are played
func (g game) playUniverseMode(cache *map[game][2]int64) (int64, int64) {
if g.player1.score >= 21 {
return 1, 0
}
if g.player2.score >= 21 {
return 0, 1
}
if cache == nil {
cache = &map[game][2]int64{}
}
// check cache
if c, ok := (*cache)[g]; ok {
return c[0], c[1]
}
var p1, p2 int64
for i1 := 1; i1 <= 3; i1++ {
for i2 := 1; i2 <= 3; i2++ {
for i3 := 1; i3 <= 3; i3++ {
newGame := g
if g.currentPlayer == 1 {
newGame.player1.position = g.player1.position + i1 + i2 + i3
for newGame.player1.position > 10 {
newGame.player1.position -= 10
}
newGame.player1.score = g.player1.score + newGame.player1.position
newGame.currentPlayer = 2
tmpP1, tmpP2 := newGame.playUniverseMode(cache)
p1 += tmpP1
p2 += tmpP2
continue
}
newGame.player2.position = g.player2.position + i1 + i2 + i3
for newGame.player2.position > 10 {
newGame.player2.position -= 10
}
newGame.player2.score = g.player2.score + newGame.player2.position
newGame.currentPlayer = 1
tmpP1, tmpP2 := newGame.playUniverseMode(cache)
p1 += tmpP1
p2 += tmpP2
}
}
}
//add to cache
(*cache)[g] = [2]int64{p1, p2}
return p1, p2
}
//getNext3DiceRoles returns the next sum of the next 3 dices and the next dice start point
func getNext3DiceRoles(cur int) (next3numbers int, nextCur int) {
cur++
if cur > 100 {
cur = 1
}
next3numbers = cur
cur++
if cur > 100 {
cur = 1
}
next3numbers += cur
cur++
if cur > 100 {
cur = 1
}
next3numbers += cur
return next3numbers, cur
}
//parseInput returns a game for the input
func parseInput(input string) game {
lines, _ := utils.InputToSlice(input)
if len(lines) != 2 {
panic("wrong input")
}
var g game
for i := range lines {
num := strings.Split(lines[i], ": ")
if len(num) != 2 {
panic("wrong input")
}
start, _ := strconv.Atoi(num[1])
if strings.HasPrefix(lines[i], "Player 1") {
g.player1 = player{
position: start,
score: 0,
}
}
if strings.HasPrefix(lines[i], "Player 2") {
g.player2 = player{
position: start,
score: 0,
}
}
}
g.currentPlayer = 1
return g
}