-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem13b.js
54 lines (51 loc) · 1.11 KB
/
problem13b.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
const fs = require('fs');
const data = fs.readFileSync('input13.txt', 'utf8');
// const data = `939
// 7,13,x,x,59,x,31,19`;
const lines = data.split('\n').map(line => line.trim()).filter(line => line.length);
// const start = parseInt(lines[0]);
const ids = lines[1].split(',').map(id => (id === 'x') ? 'x' : parseInt(id));
const pairs = [];
for (let i = 0; (i < ids.length); i++) {
if (ids[i] === 'x') {
continue;
}
pairs.push([
i,
ids[i]
]);
}
pairs.sort((a, b) => {
if (a[1] > b[1]) {
return -1;
} else if (a[1] < b[1]) {
return 1;
} else {
return 0;
}
});
let zeroTime = pairs[0][1];
let max = 0;
let broken = 0;
let increment = pairs[0][1];
console.log(pairs);
while (true) {
// console.log(zeroTime);
const now = zeroTime - pairs[0][0];
let i;
for (i = max + 1; (i < pairs.length); i++) {
if ((now + pairs[i][0]) % pairs[i][1]) {
break;
}
if (i > max) {
max = i;
increment *= pairs[i][1];
console.log(`${max} (${pairs[i][1]}) @${now}`);
}
}
if (i === pairs.length) {
console.log(now);
break;
}
zeroTime += increment;
}