-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.js
63 lines (57 loc) · 1017 Bytes
/
13.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
'use strict';
/**
* @param {string} d
*/
const part1 = async d => {
const data = d.replace(/x/g, '').replace(/,,+/g, ',').split('\n').map((e, i) => {
if (i == 1) {
return e.split(',');
}
return e;
});
const a = [0, data[0]*data[0]];
data[1].forEach(e => {
const nextStop = Math.ceil(data[0] / e) * e;
if (nextStop < a[1]) {
a[0] = e;
a[1] = nextStop;
}
});
return a[0] * (a[1] - data[0]);
};
/**
* @param {string} d
*/
const part2 = async d => {
const data = d.replace(/x/g, '-1').split('\n')[1].split(',').map(e=>+e);
let lcm = -1;
let time = -1;
let index = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
const id = data[index];
if (id == -1) {
index++;
continue;
}
if (lcm == -1) {
lcm = id;
time = id - index;
index++;
continue;
}
if ((time + index) % id == 0) {
if (++index >= data.length) {
break;
}
lcm *= id;
continue;
}
time += lcm;
}
return time;
};
module.exports = {
part1,
part2
};