Advent of Code — Day 13 — Javascript

Vincent Yang
4 min readMar 9, 2021

For day 13, you are given an input that is two lines. The first line contains a number that represents a time. The second line contains a string that has bus IDs that are separated with commas that take you to the destination. Each number in the string represents the amount of time it takes to make a full trip and return to its origin.

The problem is to find the earliest bus that will take you to the destination. In other words, assuming that you are leaving at the time represented in the first time, which bus will arrive first based on the time it takes to make a full trip. We are told that there will only be 1 correct answer based on the input data.

If we look at the sample data:

939
7,13,x,x,59,x,31,19

We can see that there are 5 buses that take us to the destination based on the second string and that the time we are leaving at is 939.

time   bus 7   bus 13  bus 59  bus 31  bus 19
929 . . . . .
930 . . . D .
931 D . . . D
932 . . . . .
933 . . . . .
934 . . . . .
935 . . . . .
936 . D . . .
937 . . . . .
938 D . . . .
939 . . . . . ---- Time leaving
940 . . . . .
941 . . . . .
942 . . . . .
943 . . . . .
944 . . D . . ---- Earliest bus
945 D . . . .
946 . . . . .
947 . . . . .
948 . . . . .
949 . D . . .

Represented visually, we see that bus 7 arrives every seven minutes and it will arrive at the times it is fully divisible by; 931, 938, 935, etc. The same goes for every other bus ID within the string.

As we are leaving at time 939, we see that the bus that will arrive the soonest will be bus ID 59 having a difference of 5 before arriving after our own departure time.

Simplifying this question, we can take the the modulus of the target and each bus ID. The will tell us how much time has passed since that particular bus last arrived. We can use that to subtract and found out how much longer it will take for the next bus to arrive. We will do this for each bus and return the smallest difference.

function day13(input){
const [departureTime, buses] = input.split('\n')
let busTime = buses.split(',')
let minArr = []
for(let i = 0; i < busTime.length; i++){
if(!Number(arr[i]){
busTime.splice(i, 1)
i--
}
}
}
//busTime = [7, 13, 59, 31, 19]

On the first line, we are destructuring the input and placing them into two variables: departureTime which holds what time we are leaving and buses which holds the string. Afterwards, we split the string and create an array of bus times. Looking back at the sample input, we have some times which are represented as ‘x’ which we should remove. For every instance in the array that is not a number, we remove it.

function day13(input){
const [departureTime, buses] = input.split('\n')
let busTime = buses.split(',')
let minArr = []
for(let i = 0; i < busTime.length; i++){
if(!Number(busTime[i]){
busTime.splice(i, 1)
i--
}
}
for(let time of busTimes){ // 7 of [7, 13, 59, 31, 19]
let mod = target % time // 939 % 7 = 1
minArr.push(time - mod) // 7 - 1 = 6
}
}
// minArr = [6, 10, 5, 22, 11]

In the second loop, we are taking each time the modulus of the target and each bus and storing the amount of time before its next arrival inside an array.

The value that Advent of Code is asking for is the bus ID times the amount of time you would have to wait for it to arrive.

function day13(input){
const [departureTime, buses] = input.split('\n')
let busTime = buses.split(',')
let minArr = []
for(let i = 0; i < busTime.length; i++){
if(!Number(busTime[i]){
busTime.splice(i, 1)
i--
}
}
for(let time of busTimes){ // 7 of [7, 13, 59, 31, 19]
let mod = target % time // 939 % 7 = 1
minArr.push(time - mod) // 7 - 1 = 6
}
let min = Math.min(...minArr)
let index = minArr.indexOf(min)
return min * arr[index]
}

--

--