-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.d
207 lines (173 loc) · 5.7 KB
/
day21.d
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
module day21;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
writeln(" ----- part 1 ----- ");
part1();
writeln(" ----- part 2 ----- ");
part2();
}
void part2() {
struct GameState {
int currentPlayer;
int player1pos;
int player2pos;
int player1total;
int player2total;
string toString() const {
return format("{c=%d p=%d,%d t=%d,%d}", currentPlayer, player1pos, player2pos, player1total, player2total);
}
}
GameState parse(string str) {
string[] pts = str.split(",");
return GameState(
pts[0].to!int,
pts[1].to!int,
pts[2].to!int,
pts[3].to!int,
pts[4].to!int,
);
}
ulong getOrDefault(ulong[GameState] states, GameState key, ulong defaultValue) {
if (key in states) {
return states[key];
}
return defaultValue;
}
ulong[GameState] states;
// example input:
// Player 1 starting position: 4
// Player 2 starting position: 8
// states[GameState(1,4,8,0,0)] = 1;
// my input:
// Player 1 starting position: 10
// Player 2 starting position: 8
states[GameState(1,10,8,0,0)] = 1;
const int scoreToWin = 21;
ulong[GameState] finalStates;
while (states.keys.length > 0) {
// writeln(states);
ulong[GameState] nextStates;
foreach(GameState prevState; states.keys) {
if (prevState.player1total >= scoreToWin || prevState.player2total >= scoreToWin) {
// Game is done, move count to finalStates.
finalStates[prevState] += states[prevState];
continue;
}
foreach (int a; 1 .. 4) {
foreach (int b; 1 .. 4) {
foreach (int c; 1 .. 4) {
int rolledSum = a + b + c;
// writefln("Dice rolled: %d, %d, %d", a, b, c);
if (prevState.currentPlayer == 1) {
int nextPlayer1pos = ((prevState.player1pos + rolledSum - 1) % 10) + 1;
int nextPlayer1total = prevState.player1total + nextPlayer1pos;
GameState nextState = GameState(
2,
nextPlayer1pos,
prevState.player2pos,
nextPlayer1total,
prevState.player2total
);
nextStates[nextState] += states[prevState];
} else {
int nextPlayer2pos = ((prevState.player2pos + rolledSum - 1) % 10) + 1;
int nextPlayer2total = prevState.player2total + nextPlayer2pos;
GameState nextState = GameState(
1,
prevState.player1pos,
nextPlayer2pos,
prevState.player1total,
nextPlayer2total
);
nextStates[nextState] += states[prevState];
}
}
}
}
}
states = nextStates;
}
states = finalStates;
ulong player1wins = 0;
ulong player2wins = 0;
foreach(GameState state; states.keys) {
if (state.player1total >= scoreToWin) {
player1wins += states[state];
} else {
player2wins += states[state];
}
}
assert(states.values.sum == player1wins + player2wins);
writefln("Unique states = %d\nTotal states = %d\nPlayer 1 wins = %d\nPlayer 2 wins = %d",
states.keys.length,
states.values.sum,
player1wins,
player2wins
);
}
void part1() {
int[int] players;
// example input:
// Player 1 starting position: 4
// Player 2 starting position: 8
// players[1] = 4;
// players[2] = 8;
// my input:
// Player 1 starting position: 10
// Player 2 starting position: 8
players[1] = 10;
players[2] = 8;
Die die = new Die(100, 100);
int[int] playerTotals;
playerTotals[1] = 0;
playerTotals[2] = 0;
int currentPlayer = 1;
int step = 0;
while (step < 10_000) {
int[] rolledValues = [die.roll, die.roll, die.roll];
players[currentPlayer] += rolledValues.sum;
players[currentPlayer] = ((players[currentPlayer] - 1) % 10) + 1;
playerTotals[currentPlayer] += players[currentPlayer];
/*writef("Player %s rolls %s and moves to space %d for a total score of %d.\n",
currentPlayer, rolledValues, players[currentPlayer], playerTotals[currentPlayer]
);*/
if (currentPlayer == 1) {
currentPlayer = 2;
} else {
currentPlayer = 1;
}
if (playerTotals[1] >= 1000) {
writef("%d * %d = %d\n",
playerTotals[2], die.timesRolled, playerTotals[2] * die.timesRolled
);
break;
}
if (playerTotals[2] >= 1000) {
writef("%d * %d = %d\n",
playerTotals[1], die.timesRolled, playerTotals[1] * die.timesRolled
);
break;
}
step++;
}
}
class Die {
uint current;
uint max;
uint timesRolled = 0;
this(uint current, uint max) {
this.current = current;
this.max = max;
}
uint roll() {
timesRolled++;
current = ((current - 1 + 1) % 100) + 1;
return current;
}
}