-
Notifications
You must be signed in to change notification settings - Fork 2
/
day9.js
62 lines (53 loc) · 1.82 KB
/
day9.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
const fs = require('fs').promises;
let readInput = async () => {
let res = await fs.readFile('./input9.txt');
// let res = await fs.readFile('./testInput.txt');
let inputs = res.toString().split('\n');
return inputs.filter(s => s[0] !== '/');
}
let play = (n, marble) => {
let scoreboard = Array(n).fill(0);
let nextMarble = 0;
// let node: value, left, right
let current = { value: nextMarble }
current.left = current;
current.right = current;
nextMarble++;
let player = 0;
while (nextMarble <= marble*100) {
if (nextMarble % 23 !== 0) {
// place clockwise after 1 marble
let left = current.right;
let right = current.right.right;
let newMarble = { value: nextMarble, left, right };
left.right = newMarble;
right.left = newMarble;
current = newMarble;
} else {
scoreboard[player] += nextMarble;
//remove 7 counter clockwise
for (let i = 1; i < 7; i++) {
current = current.left;
}
let removed = current.left;
removed.left.right = current;
current.left = removed.left;
scoreboard[player] += removed.value;
}
player = (player + 1) % n;
nextMarble++;
}
return scoreboard;
}
let main = async () => {
let inputs = (await readInput());
for (let line of inputs) {
// 9 players; last marble is worth 25 points: high score is 32
line = line.split(' ');
let [numberOfPlayers, lastMarble] = [line[0], line[6]].map(Number);
console.log(`We are playing with ${numberOfPlayers} players and ${lastMarble} marbles.`);
let scoreboard = play(numberOfPlayers, lastMarble);
console.log(Math.max(...scoreboard));
}
}
main();