-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathday13.js
61 lines (48 loc) · 1.26 KB
/
day13.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
const fs = require('fs');
const lines = fs.readFileSync('day13.txt', {encoding: 'utf-8'}).split('\n').filter(x => x);
const time = parseInt(lines[0]);
const buses = lines[1].split(',');
const list = [];
buses.forEach(bus => {
if(bus == 'x') return;
const id = parseInt(bus);
list.push({
bus: id,
nextOne: id - time % id,
});
})
list.sort((a, b) => a.nextOne - b.nextOne);
console.log(list[0].bus * list[0].nextOne);
const timestamps = [];
buses.forEach((bus, index) => {
if(bus != 'x') timestamps.push({id: parseInt(bus), delta: index});
});
// 7,13,x,x,59,x,31,19
// t = 7*x
// t+1 = 13*y
// t+4 = 59*z
let step = timestamps[0].id;
let t = step;
// for(let t=step; ;t+=step) {
// let failed = false;
// for(let j=1; j<timestamps.length; j++) {
// if((t+timestamps[j].delta) % timestamps[j].id !== 0) {
// failed = true;
// break;
// } else {
// console.log(t);
// step *= timestamps[j].id;
// }
// }
// if(!failed) {
// console.log(t);
// break;
// }
// }
for(let j=1; j<timestamps.length; j++) {
while((t+timestamps[j].delta) % timestamps[j].id !== 0) {
t += step;
}
step *= timestamps[j].id;
}
console.log(t);