Advent of Code, Day 13

Day 13 of Advent of Code is here!

See all of my other solutions here. If you’re interested, check out what my colleagues at Slalom are doing with Advent of Code!

Do your best!

I highly encourage you to get through the day’s challenge on your own. I’m finding these are getting more complex, not impossible, just taking longer to complete. Like Day 13 and 14, and others I’m sure, getting through these is taking more time than I have (full time job, 4 kids, and other hobbies). I’ll continue to do my best, but I may start deferring to some of my colleague’s code.

Day 13: Shuttle Search

Part I

See day 13 for the storyline. These stories are getting too big to bring into my blog, I hope you understand :D

Before we start, let’s get pull this data in. It’s just 2 lines today. Don’t worry, plenty of looping to be had.

Pulling in the above values from my input file

const fs = require('fs');
const buses = fs.readFileSync('./input.txt', 'utf-8').trim().split('\n')

Then let’s get our start time (line 1) and the bus ids (line 2)

const startTime = Number(buses[0])
const busIds = buses[1].split(',').filter(b => b !== 'x').sort((a, b) => a > b ? 1 : -1).map(id => Number(id))

We can take that long string and split it on ,. For Part 1, we don’t need to worry about the x in the value, so we can filter() that right out. Then I sort it by the bus id to get a nice ordered list. Finally, I convert the strings to numbers.

Now let’s loop through the bus ids and find the next bus id we can take

let nextTime = startTime

let busFound = false
while(!busFound) {
  const ourBus = busIds.find(bus => nextTime % bus === 0)
  if(ourBus) {
    busFound = true
    const waiting = nextTime - startTime
    console.log('we can pick up', ourBus, 'at', nextTime, 'waiting', waiting)
    console.log(ourBus * waiting, 'is the answer!')
  }
  nextTime += 1
}

Short and sweet

  • We’re using a while() here, as along as we haven’t found the bus, let’s loop through the ids
  • As we loop, we’re incrementing our time, we start at our start time, and try to find any bus ids that give us a remainder (using %) of 0 (which means the time divides into the number evenly)
  • Once we find ourBus, we calculate how much time we waited for this bus, and get the answer!

Part II

Sadly, I wasn’t able to figure out Part 2 in a reasonable amount of time. Fortunately, my teammates did. Big thanks to Josh for getting it done!

const fs = require('fs');
let notes = fs.readFileSync('./input.txt', 'utf-8').trim().split('\n');

const busses = notes[1].split(',').map(b => b === 'x' ? -1 : parseInt(b));

let minT = 0;
let t = minT;

const lcmPrime = i => busses.filter(b => b > 0).slice(0, i).reduce((a, b) => a * b, 1);

let inc = lcmPrime(1);
let numMatch = 1;

const test = (t) => {
	let localMatch = 0

	for (var i = 0; i < busses.length; i++) {
		if (busses[i] > 0) {
			if ((t + i) % busses[i] === 0) {
				// This is the critical section that ups the 
				// increment as we get more and more matches
				localMatch++;
				if (localMatch > numMatch) {
					numMatch++;
					inc = lcmPrime(numMatch);
				}
			}
			else {
				return false;
			}
		}
	}

	return true;
};

// Find the first place to start
while(t % inc !== 0) t++;

// Update and increment
while(!test(t)) t += inc;

console.log('PART 2', t);

Take a look through and see what he’s doing there. I don’t know entirely what’s going on here. I do apologize for not being able to spend more time on this puzzle. I only have so much time in the day… :(

How did it go for you?

How did you find the answer on your own? How did you do it? Anything stump you? I’d love to hear how you did! Please comment below! I have also loaded up just the challenge and code from this and my other days on github, you can follow along there too: pretty Git pages or the code in the repo.

Series Navigation<< Advent of Code, Day 14Advent of Code, Day 12 >>

Leave a Reply

Up ↑

Discover more from David Lozzi

Subscribe now to keep reading and get access to the full archive.

Continue reading