-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-1.js
76 lines (67 loc) · 2.12 KB
/
09-1.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
function main() {
function getHighScore(input) {
const matchGroups = input.match(
/^(?<playerCount>\d+) players;.*?worth (?<lastMarbleScore>.*?) .*?$/
).groups;
const playerCount = parseInt(matchGroups.playerCount, 10);
const lastMarbleScore = parseInt(matchGroups.lastMarbleScore, 10);
const circle = [0, 1];
const scoresByPlayer = {};
let playerIndex = 2;
for (let i = 2, current = 1; i <= lastMarbleScore; i++) {
if (i % 23 === 0) {
if (!scoresByPlayer[playerIndex]) {
scoresByPlayer[playerIndex] = 0;
}
let index = current - 7;
if (index < 0) {
index = circle.length - (Math.abs(index) % circle.length);
}
const removedMarble = circle.splice(index, 1);
scoresByPlayer[playerIndex] += i + removedMarble[0];
current = index;
} else {
let index = current + 2;
if (index > circle.length) {
index %= circle.length;
}
circle.splice(index, 0, i);
current = index;
}
playerIndex = (playerIndex + 1) % playerCount;
}
let maxScore = 0;
Object.keys(scoresByPlayer).forEach(playerIndex => {
maxScore = Math.max(maxScore, scoresByPlayer[playerIndex]);
});
return maxScore;
}
console.clear();
console.log(
'Test (9 players):',
getHighScore('9 players; last marble is worth 25 points') === 32
);
console.log(
'Test (10 players):',
getHighScore('10 players; last marble is worth 1618 points') === 8317
);
console.log(
'Test (13 players):',
getHighScore('13 players; last marble is worth 7999 points') === 146373
);
console.log(
'Test (17 players):',
getHighScore('17 players; last marble is worth 1104 points') === 2764
);
console.log(
'Test (21 players):',
getHighScore('21 players; last marble is worth 6111 points') === 54718
);
console.log(
'Test (30 players):',
getHighScore('30 players; last marble is worth 5807 points') === 37305
);
console.log('Result:', getHighScore(givenInput));
}
const givenInput = '477 players; last marble is worth 70851 points';
main();