Day 6, part 1
Day 6 challenge of the mighty Advent of Code 2024
Created at:
Last updated:
Table of Contents
Introduction
Hello and welcome to the day 6 of Advent of Code. Before you continue on reading this article, please read carefully the description of this puzzle here. Today we are participating in boats race. We are joining multiple races, where each of them has a fixed duration and in this time we have to travel further then the previous records established (our input data). Our boat has a button which increases the speed but during the time we press the button, the boat is not moving, so we have to find a balance in time spend on increasing the speed and moving the boat. Each race can be beaten in couple different ways either by spending more time on increasing the speed or on letting the boat go earlier with lower speed but for longer time. We are asked to calculate how many options we have in each race to still win the race and then multiply the amount of options in each race, to get the final result.
Creating files
We are going to start by creating the new directory in or project's root directory.
We also create new file inside the folder, which will be our main entry for the code for this puzzle.
We can also put the test input data inside the .
Solution
We can divide the problem into steps like so:
- Parse the input data into race objects with and properties.
- Find the minimal time for which we have to press the button to win.
- Find the maximal time for which we have to press the button to win.
- Calculate how many options we have with step of one millisecond between the min and max button press time.
- Multiply results collected for each race to get the final answer.
Parsing the data
This task can be achieved by splitting the input into time and distance lines and then extracting and parsing the numbers.
And in the terminal we can see the following output:
Looks about right, so we can commit the changes.
Calculating the minimal button pressing time
Looking at the problem from the mathematics side of things, the whole problem can be described by the following equation: (v _ t1) _ (t - t1) > s where: - velocity - time of the button being pressed - total race time - record distance to be beaten As you can see this is a quadratic inequality. To get the min and max time of the button being pressed, we have to find the roots of this equation rounded to the full millisecond. The amount of milliseconds with the step of 1 millisecond between these two points is the amount of solutions with which we still win.
In the code above we basically solve the quadratic equation by finding it roots. We also make sure that our solutions are within the roots range as our main function is an inequality.
If we now take a look at the puzzle description we can find there this analysis (I allowed myself to just copy the description from the website):
- Since the current record for this race is 9 millimeters, there are actually 4 different ways you could win: you could hold the button for 2, 3, 4, or 5 milliseconds at the start of the race.
- In the second race, you could hold the button for at least 4 milliseconds and at most 11 milliseconds and beat the record, a total of 8 different ways to win.
- In the third race, you could hold the button for at least 11 milliseconds and no more than 19 milliseconds and still beat the record, a total of 9 ways you could win. As you can see we have exactly these bounding numbers. We can same the changes now before we move forward.
Counting all the options
This is as easy as subtract one root from the other. We need to also do as our min and max values are included in the result.
What we expect to see is:
- 4 solutions for the first race
- 8 solutions for the second race
- 9 solutions for the third race When we run the code now, that's what we can see in the console:
Looks good to me! We can commit the changes and move forward.
Getting the final result
The final result is calculated simply by multiplying count of solutions of each race.
And this is the expected result. We can save this and test our code with the real data.
Final result
This is the time to test our code in battle. Once again we will use the function, to get the real input data, which we copy from the Advent of Code website and we put the data inside file.
Now we run the code and we can see in the terminal:
Lets now submit this on the website and...
Amazing! Well done!
We can happily save our final result!
And please now join me in the part 2! Day 6, part 2