
Day 6, part 2
Day 6 challenge of the mighty Advent of Code 2024
Introduction
Once again, let me welcome you to the next Advent of Code puzzle. This is a part 2 of day 6 and this article is a continuation of the Day 6, part 1, so please read it first before you start this one. 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 is the new problem
Well, it came out that the input data is not listing couple of separate races, but this is just one race (one time and one distance), but the numbers just have some spaces in-between. So lets look at the test data:
1Time: 7 15 30
2Distance: 9 40 200With what we know now, these numbers supposed to be read as Time: 71530 and Distance: 940200. Looking at the website for the solutions to this example we can see that the range starts from 14 until 71516 and the final count of all solutions is 71503.
Solution
The solution is pretty simple and it in the most part reuses all those functions we had before. I just had to write a new data parsing function.
Please replace input data from the file with the test data.
Here is the whole new code to get the.
1function parseTextToData2(text) {
2 const [timeString, distanceString] = text.split('\
3');
4 const time = stringToNumber(timeString.split('Time:')[1]);
5 const distance = stringToNumber(distanceString.split('Distance:')[1]);
6 return {
7 time,
8 distance,
9 velocity: 1,
10 };
11}
12function stringToNumber(numberString) {
13 return parseInt(numberString.trim().split(/\\s+/).join(''));
14}
15const newInputData = parseTextToData2(input);
16const newRoot = findMinAndMaxTimeForRace(newInputData);
17const newFinalResult = getSolutionCount(newRoot);
18console.log('New final result: ', newFinalResult);If we run this piece of the code we will see:
1New final result: 71503And this is what we were looking for! Let's save it!
1git add .
2git commit -m "use input as single race data"Final result
Now we can switch back to the input being read from the file and we can make the final run of our code.
1New final result: 35349468We now submit this number on the page and we can see our favorite message.
Lets commit all the changes.
1git add .
2git commit -m "day 6 part 2 final result"This is all if it comes to day 6. Pretty easy huh? I hope you enjoyed it and see you in Day 7, part 1