-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.js
34 lines (26 loc) · 824 Bytes
/
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
const timetable = require('./entries/day13.js').default;
let [departingTimestamp, buses] = timetable.split('\n');
departingTimestamp = parseInt(departingTimestamp);
buses = buses.split(',').map(bus => (bus === 'x' ? null : parseInt(bus)));
// PART 1
let busIDToAirport = null;
let currentTimestamp = departingTimestamp;
while (!busIDToAirport) {
busIDToAirport = buses.find(bus => !!bus && currentTimestamp % bus === 0);
currentTimestamp++;
}
console.log((currentTimestamp - 1 - departingTimestamp) * busIDToAirport);
// PART 2
let [firstBus, ...restOfBuses] = buses;
let timestamp = 0;
restOfBuses.forEach((bus, offset) => {
if (!bus) return;
while (true) {
if ((timestamp + offset + 1) % bus === 0) {
firstBus *= bus;
break;
}
timestamp += firstBus;
}
});
console.log(timestamp);