
Day 8, part 1
Day 8 challenge of the mighty Advent of Code 2024
Introduction
Today's challenge is about finding the path on a map which looks something like this:
1RL
2AAA = (BBB, CCC)
3BBB = (DDD, EEE)
4CCC = (ZZZ, GGG)
5DDD = (DDD, DDD)
6EEE = (EEE, EEE)
7GGG = (GGG, GGG)
8ZZZ = (ZZZ, ZZZ)It is build out of two parts: the instructions and nodes.
Starting with AAA, you need to look up the next element based on the next left/right instruction in your input. In this example, start with AAA and go right (R) by choosing the right element of AAA, CCC. Then, L means to choose the left element of CCC, ZZZ.
As the result we have to count how many steps we had to take to make it from AAA to ZZZ
Solution
To get what we need, we can divide our problem into steps:
- Parse input data - without this it is hard to process it further
- Take the
AAAnode as a starting point and based on theRLinstructions move through the nodes until you get toZZZ.
Seems to be pretty straight forward, doesn't it?
Parse input data
So first we are taking the test data in a form of text, and we want to get something more useful out of it.
From the first line we extract the instructions into an array and the rest is our nodes' data which we transform into node lookUps. Each lookUp has its name and indicates which node would we get going left and which going right. These properties are named L and R to match the instruction names to make it easier to move through the data later.
1const input = `RL
2AAA = (BBB, CCC)
3BBB = (DDD, EEE)
4CCC = (ZZZ, GGG)
5DDD = (DDD, DDD)
6EEE = (EEE, EEE)
7GGG = (GGG, GGG)
8ZZZ = (ZZZ, ZZZ)`;
9const FIRST_NODE_NAME = 'AAA';
10const LAST_NODE_NAME = 'ZZZ';
11function parseInputData(input) {
12 const [instructionsText, lookUpText] = input.split('\
13\
14');
15 const instructions = instructionsText.trim().split('');
16 const lookUps = parseLookUps(lookUpText);
17 return { instructions, lookUps };
18}
19function parseLookUps(lookUpText) {
20 const lines = lookUpText.split('\
21');
22 return lines.map((line) => {
23 const [node, L, R] = line.replace(/(\\(|\\)|,|=)/g, '').split(/\\s+/);
24 return { node, L, R };
25 });
26}
27const inputData = parseInputData(input);
28console.log('Parsed input data: ', inputData);Let's run this and see if what we get is what we were looking for.
1Parsed input data: {
2 instructions: [ 'R', 'L' ],
3 lookUps: [
4 { node: 'AAA', L: '(BBB,', R: 'CCC)' },
5 { node: 'BBB', L: '(DDD,', R: 'EEE)' },
6 { node: 'CCC', L: '(ZZZ,', R: 'GGG)' },
7 { node: 'DDD', L: '(DDD,', R: 'DDD)' },
8 { node: 'EEE', L: '(EEE,', R: 'EEE)' },
9 { node: 'GGG', L: '(GGG,', R: 'GGG)' },
10 { node: 'ZZZ', L: '(ZZZ,', R: 'ZZZ)' }
11 ]
12}This is exactly what we expected, so we can commit these changes before moving forward.
1git add .
2git commit -m "parse instructions and lookups"Crawl the desert
Now when we have our data in easily digestible form, we can start crawling through the nodes until we get to the final destination. Basically this whole process can be split into steps:
- We get the current node object based on its name
- We check if we didn't get to the end of the instructions - if so, we start form the begging.
- We find the next node based on the current instruction and the current node.
- We keep counting steps during this process. The code which does that looks like this:
1function crawlTheDesert(instructions, lookUps) {
2 let steps = 0;
3 let currentNode = getNodeByName(FIRST_NODE_NAME, lookUps);
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 === LAST_NODE_NAME;
26}
27const { instructions, lookUps } = inputData;
28const steps = crawlTheDesert(instructions, lookUps);
29console.log('Steps to crawl the desert: ', steps);Looking at the test data and solving the puzzle by hand, we can see that the resulting amount of steps here is 2. So lets run this piece of code and see if our assumption is correct.
1Steps to crawl the desert: 2Looks like we did it! We can save our progress.
1git add .
2git commit -m "crawl the desert"Final result
This is the time to test our code in battle. Once again we will use the readFile function, to get the real input data, which we copy from the Advent of Code website and we put the data inside input.txt file.
1const fs = require('fs');
2function readFile(filename) {
3 return fs.readFileSync(filename, 'ascii');
4}
5const input = readFile('input.txt');Now we run the code and we can see in the terminal:
1Steps to crawl the desert: 20513If we submit the answer we can see:
Great! It looks like we are right once again!
We can save the final result.
1git add .
2git commit -m "day 8 part 1 final result"And I hope to see you in 16. day 8, part 2!