-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.js
177 lines (152 loc) · 3.67 KB
/
day21.js
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
const { test, data } = require('./data/day21');
const advanceToken = (starting, count) => {
const advanceTo = (starting + count) % 10;
if (advanceTo === 0) {
return 10;
}
return advanceTo;
};
const generateDeterministicDieRoll = () => {
let current = 0;
return () => {
if (current === 100) {
current = 1;
return current;
}
current++;
return current;
};
};
const playGame = (playerOneStarting, playerTwoStarting) => {
const rollDie = generateDeterministicDieRoll();
const scores = {
'1': 0,
'2': 0,
};
const positions = {
'1': playerOneStarting,
'2': playerTwoStarting,
};
let playerOneTurn = true;
let turns = 0;
while (scores[1] < 1000 && scores[2] < 1000) {
const activePlayer = playerOneTurn ? '1' : '2';
const roll = Array.from({ length: 3 }).reduce((sum) => {
const nextDie = rollDie();
return sum + nextDie;
}, 0);
const nextPosition = advanceToken(positions[activePlayer], roll);
scores[activePlayer] += nextPosition;
positions[activePlayer] = nextPosition;
turns++;
playerOneTurn = !playerOneTurn;
}
const diceRolls = turns * 3;
return Math.min(...Object.values(scores)) * diceRolls;
};
/**
* Each step
* 1 -> 1 -> 1, 3
* 1 -> 1 -> 2, 4
* 1 -> 1 -> 3, 5
* 1 -> 2 -> 1, 4
* 1 -> 2 -> 2, 5
* 1 -> 2 -> 3, 6
* 1 -> 3 -> 1, 5
* 1 -> 3 -> 2, 6
* 1 -> 3 -> 3, 7
*
* 2 -> 1 -> 1, 4
* 2 -> 1 -> 2, 5
* 2 -> 1 -> 3, 6
* 2 -> 2 -> 1, 5
* 2 -> 2 -> 2, 6
* 2 -> 2 -> 3, 7
* 2 -> 3 -> 1, 6
* 2 -> 3 -> 2, 7
* 2 -> 3 -> 3, 8
*
* 3 -> 1 -> 1, 5
* 3 -> 1 -> 2, 6
* 3 -> 1 -> 3, 7
* 3 -> 2 -> 1, 6
* 3 -> 2 -> 2, 7
* 3 -> 2 -> 3, 8
* 3 -> 3 -> 1, 7
* 3 -> 3 -> 2, 8
* 3 -> 3 -> 3, 9
*
* Advance squares: Universe spawn count
* 3: 1
* 4: 3
* 5: 6
* 6: 7
* 7: 6
* 8: 3
* 9: 1
*
*/
const advanceObj = {
3: 1,
4: 3,
5: 6,
6: 7,
7: 6,
8: 3,
9: 1,
};
const advanceDiracGame = (diracObject, isPlayerOneTurn) => (
Object.entries(diracObject).reduce((agg, [key, count]) => {
const [ p1Score, p1Pos, p2Score, p2Pos ] = key.split(',').map(Number);
Object.entries(advanceObj).forEach(([advanceSteps, advanceCount]) => {
if (isPlayerOneTurn) {
const nextP1Pos = advanceToken(p1Pos, Number(advanceSteps));
const nextP1Score = p1Score + nextP1Pos;
const nextCount = count * advanceCount;
const key = [nextP1Score, nextP1Pos, p2Score, p2Pos].join();
agg[key] = (agg[key] ?? 0) + nextCount;
return;
}
const nextP2Pos = advanceToken(p2Pos, Number(advanceSteps));
const nextP2Score = p2Score + nextP2Pos;
const nextCount = count * advanceCount;
const key = [p1Score, p1Pos, nextP2Score, nextP2Pos].join();
agg[key] = (agg[key] ?? 0) + nextCount;
});
return agg;
}, {})
);
const playDiracGame = (playerOneStarting, playerTwoStarting) => {
const startingKey = [0, playerOneStarting, 0, playerTwoStarting].join();
const starting = {
[startingKey]: 1,
};
let playerOneTurn = true;
let next = starting;
const wins = {
p1: 0,
p2: 0,
};
while (Object.keys(next).length > 0) {
next = advanceDiracGame(next, playerOneTurn);
Object.keys(next).forEach((key) => {
const [ p1Score, p1Pos, p2Score, p2Pos ] = key.split(',').map(Number);
if (p1Score >= 21) {
wins.p1 += next[key];
delete next[key];
return;
}
if (p2Score >= 21) {
wins.p2 += next[key];
delete next[key];
return;
}
});
playerOneTurn = !playerOneTurn;
}
return wins;
};
console.log(playGame(4, 8));
console.log(playGame(7, 4));
console.log(playDiracGame(4, 8));
console.log(playDiracGame(7, 4));