
Day 8, part 2
Day 8 challenge of the mighty Advent of Code 2024
Introduction
Welcome to the par two of day 8 puzzle. First part is described in Day 8, part 1. Please get familiar with the part 1 before jumping into this article. This part of the challenge is available only when you successfully finished the part 1. The second part of the challenge is described under -- Part Two -- section here.
What's the new problem?
From now on the rules changed. We don't have one starting and one ending point, thus there is more than one path. Now every node which name ends with A is a start node and every node which name ends with Z is an end node. We have to go through every path starting at every starting node and finish when all the paths end simultaneously on a Z node. Once again, the answer we have to give is how many steps it took.
Solution
To solve this problem we will mostly reuse the code we have, we just have to make some adjustments.
Update the crawl function to take the initial node as a parameter
Now when we have to crawl multiple paths from many different starting points, it might be useful to pass to our crawlTheDesert function the initial node. But first let's update the test data with the one from this part of the puzzle.
1const input = `LR
211A = (11B, XXX)
311B = (XXX, 11Z)
411Z = (11B, XXX)
522A = (22B, XXX)
622B = (22C, 22C)
722C = (22Z, 22Z)
822Z = (22B, 22B)
9XXX = (XX, XXX)`;We also have to change the rules of what means that the node is last. Now we only need our node to end up with Z.
1function isLastNode(node) {
2 return node.node.endsWith('Z');
3}Now if we take the node 11A as a staring node and look at its path it goes like this:
111A -L-> 11B -R-> 11ZThis means we had to take 2 steps to get to the node which ends with Z.
Now lets update the crawlTheDesert function to take the initialNode parameter.
1function crawlTheDesert(instructions, lookUps, initialNode) {
2 let steps = 0;
3 let currentNode = initialNode;
4 let currentInstructionIndex = 0;
5 while (!isLastNode(currentNode)) {
6 if (currentInstructionIndex >= instructions.length) {
7 currentInstructionIndex = 0;
8 }
9 const currentInstruction = instructions[currentInstructionIndex];
10 const nextNodeName = getNextNodeName(currentInstruction, currentNode);
11 const nextNode = getNodeByName(nextNodeName, lookUps);
12 currentNode = nextNode;
13 currentInstructionIndex++;
14 steps++;
15 }
16 return steps;
17}
18function getNextNodeName(instruction, node) {
19 return node[instruction];
20}
21function getNodeByName(name, nodes) {
22 return nodes.find((node) => node.node === name);
23}
24function isLastNode(node) {
25 return node.node.endsWith('Z');
26}
27const { instructions, lookUps } = inputData;
28const steps = crawlTheDesert(instructions, lookUps, getNodeByName('11A', lookUps));
29console.log('Steps to crawl the desert: ', steps);And if we run the code we can see:
1Steps to crawl the desert: 2This means our code works fine. We can save this and move forward.
1git add .
2git commit -m "add initial node argument to crawl function"Finding the starting nodes
This is as simple as finding all the nodes which name ends with A.
1function getStartingNodes(nodes) {
2 return nodes.filter((node) => node.node.endsWith('A'));
3}
4const startingNodes = getStartingNodes(lookUps);
5console.log('All starting nodes: ', startingNodes);This gives us the following list of nodes:
1All starting nodes: [
2 { node: '11A', L: '11B', R: 'XXX' },
3 { node: '22A', L: '22B', R: 'XXX' }
4]If you look now at the test data, you can see that this is the correct answer. We can commit the changes.
1git add .
2git commit -m "find staring nodes"Finding the number of steps for each path
Now We can use our upgraded crawlTheDesert function to find how many steps we need to make to
1const stepsForEachNode = startingNodes.map((node) => crawlTheDesert(instructions, lookUps, node));
2console.log('Steps for each path: ', stepsForEachNode);As you can see this is almost the same function as the crawlTheDesert, but we changed the condition to check if all the currentNodes are the ending nodes and in general we update multiple nodes instead of just one. Now if we run our little program we can see:
1Steps for each path: [ 14893, 20513, 22199, 19951, 17141, 12083 ]If you take a look into the puzzle description, you will see that this is the correct answer.
Once again we can save our progress.
1git add .
2git commit -m "find min num of steps for all paths to finish"Finding the LCM
Instead of looping until we get the result where all the nodes ended up on Z node, we can find the least common multiple for all the steps we had to take in each path. To do this we can use this set of functions:
1function leastCommonMultiple(array) {
2 let n = 1;
3 for (let i = 0; i < array.length; ++i) {
4 n = leastCommonMultipleOf2(array[i], n);
5 }
6 return n;
7}
8function leastCommonMultipleOf2(a, b) {
9 return (a * b) / greatestCommonDivisorOf2(a, b);
10}
11function greatestCommonDivisorOf2(a, b) {
12 if (!b) return b === 0 ? a : NaN;
13 return greatestCommonDivisorOf2(b, a % b);
14}
15const stepsToTakeToFinishAllPaths = leastCommonMultiple(stepsForEachNode);
16console.log('Steps to finish all paths:', stepsToTakeToFinishAllPaths);We can run this and see if the result is as we expected. But what do we expect? Lets take a look at the test data.
- We start at
11Aand22A. - We move both nodes to the left, leading to
11Band22B. - Next we move both nodes to the right, leading to
11Zand22C. - Once again, we move both nodes to the left, leading to
11Band22Z. - Then, we move both nodes to the right, leading to
11Zand22B. - Once more, we move both nodes to the left, leading to
11Band22C. - And finally we move both nodes to the right, leading to
11Zand22Z. So, in this example, you end up entirely on nodes that end inZafter6steps.
1Steps to finish all paths: 6So that's it! Let's save the result of our work before we feed our code with the real data.
1git add .
2git commit -m "crawl the desert"Final result
Now if we replace the test data with the one coming from the input file and run our program we get:
1Steps to finish all paths: 15995167053923We can feed this number to the form on the AoC website and as soon as we submit it we can see that our hard work was a big success. Congratulations to all of you who made it.
The final step is to save our progress.
1git add .
2git commit -m "day 8 part 2 final result"Now it's time for me to invite you to the Day 9, part 1 where we will try to crack the code once again!