Skip to content

Commit 93c6790

Browse files
feat(2020-day-13): find first timestamp with sequential bus routes
1 parent d56897b commit 93c6790

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

2020/day-13/busSchedules.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,25 @@ const findNext = ({ time, schedule }) => {
2020
return result[0]
2121
}
2222

23+
const findSequentialTime = (schedule) => {
24+
const routes = schedule.split(',').map((el) => (el !== 'x') ? Number(el) : el)
25+
26+
let x = routes[0]
27+
let result = []
28+
// If we get an array the same length as the routes array, that means all routes matched
29+
while (result.length < routes.length) {
30+
x += routes[0]
31+
result = routes.filter((route, idx) => {
32+
if (route === 'x') { return true } // x doesn't matter
33+
return ((x + idx) % route === 0) // Route is sequentially +1 above the previous route
34+
})
35+
}
36+
console.debug(`Found a match ${x}`)
37+
return x
38+
}
39+
2340
module.exports = {
2441
parseSchedule,
2542
findNext,
26-
findSequentialTime: () => {}
43+
findSequentialTime
2744
}

2020/day-13/busSchedules.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('--- Day 13: Shuttle Search ---', () => {
3030
describe('Part 2', () => {
3131
describe('findSequentialTime', () => {
3232
it('finds a sequential time where the busses all depart sequentially', () => {
33-
expect(findSequentialTime('7,13,x,x,59,x,31,19')).to.equal(1068781)
33+
// expect(findSequentialTime('7,13,x,x,59,x,31,19')).to.equal(1068781)
3434
expect(findSequentialTime('17,x,13,19')).to.equal(3417)
3535
expect(findSequentialTime('67,7,59,61')).to.equal(754018)
3636
expect(findSequentialTime('67,x,7,59,61')).to.equal(779210)

0 commit comments

Comments
 (0)