Skip to content

Commit 5dd4d89

Browse files
feat(2020-day-13): find the next bus to the airport
1 parent 8af9d7d commit 5dd4d89

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

2020/day-13/busSchedules.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const parseSchedule = (schedule) => {
2+
// Drop "x" and convert to numeric
3+
return schedule.split(',').filter((e) => e !== 'x').map(Number)
4+
}
5+
6+
const findNext = ({ time, schedule }) => {
7+
const result = schedule.map((route) => {
8+
return {
9+
route,
10+
wait: route - (time % route)
11+
}
12+
}).sort(
13+
(a, b) => a.wait - b.wait
14+
)
15+
16+
result.forEach((res) => {
17+
console.debug(`Wait for route ${res.route} is ${res.wait}`)
18+
})
19+
20+
return result[0]
21+
}
22+
23+
module.exports = {
24+
parseSchedule,
25+
findNext
26+
}

2020/day-13/busSchedules.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { parseSchedule, findNext } = require('./busSchedules')
4+
5+
const testData = {
6+
time: 939,
7+
schedule: '7,13,x,x,59,x,31,19'
8+
}
9+
10+
describe.only('--- Day 13: Shuttle Search ---', () => {
11+
describe('Part 1', () => {
12+
describe('parseSchedule()', () => {
13+
it('cleans up the schedule', () => {
14+
expect(parseSchedule(testData.schedule))
15+
.to.deep.equal([7, 13, 59, 31, 19])
16+
})
17+
})
18+
describe('findNext()', () => {
19+
it('finds the next departing bus', () => {
20+
expect(findNext({
21+
time: 939,
22+
schedule: parseSchedule(testData.schedule)
23+
})).to.deep.equal({
24+
route: 59,
25+
wait: 5
26+
})
27+
})
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)