-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartOne.js
30 lines (26 loc) · 923 Bytes
/
partOne.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
const partOne = (input) => {
const [players, marbles] = input.split(/\D+/).map(Number);
const scores = new Uint32Array(players);
const circle = new Uint32Array(marbles);
let circleLength = 1;
let current = 0;
for (let value = 1; value <= marbles; value++) {
if (value % 23) {
current = ((current + 1) % circleLength) + 1;
// Array.prototype.copyWithin(): https://www.geeksforgeeks.org/javascript-array-copywithin/
circle.copyWithin(current + 1, current, circleLength);
circle[current] = value;
circleLength++;
} else {
const player = value % players;
let score = value;
current = current > 6 ? current - 7 : current + circleLength - 7;
score += circle[current];
circle.copyWithin(current, current + 1, circleLength);
scores[player] += score;
circleLength--;
}
}
return Math.max(...scores);
};
module.exports = partOne;