-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpart-one.js
50 lines (40 loc) · 1.05 KB
/
part-one.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
const { input } = require('./input');
const { LoopedList } = require('looped-list');
// `Math.max(...arr)` throws a RangeError when too many arguments are passed.
const findMax = (arr) => {
let max = Number.MIN_SAFE_INTEGER;
for (let v of arr) {
if (v > max) {
max = v;
}
}
return max;
};
const list = new LoopedList(input);
const MAX_VALUE = findMax(input);
for (let i = 0; i < 100; i++) {
let current_cup_item = list.head;
let current_cup = list.head.value;
list.move(1);
let a = list.popHeadMoveNext().value;
let b = list.popHeadMoveNext().value;
let c = list.popHeadMoveNext().value;
let next_cup = current_cup - 1;
let next_cup_item = list.find(next_cup);
while (!next_cup_item) {
next_cup--;
if (next_cup < 1) {
next_cup = MAX_VALUE;
}
next_cup_item = list.find(next_cup);
}
list.setHead(next_cup_item);
list.insertNext(a);
list.insertNext(b);
list.insertNext(c);
list.setHead(current_cup_item);
list.move(1);
}
list.setHead(list.find(1));
list.popHeadMoveNext();
console.log(parseInt([...list.values()].join(''), 10));