Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2023 Day 1
- Advent of Code 2023 Day 2
- Advent of Code 2023 Day 3
- Advent of Code 2023 Day 4
- Advent of Code 2023 Day 5
- Advent of Code 2023 Day 6
- Advent of Code 2023 Day 7
- Advent of Code 2023 Day 8
- Advent of Code 2023 Day 9
- Advent of Code 2023 Day 10
- Advent of Code 2023 Day 11
- Advent of Code 2023 Day 12
- Advent of Code 2023 Day 13
- Advent of Code 2023 Day 14
- Advent of Code 2023 Day 15
- Advent of Code 2023 Day 16
- Advent of Code 2023 Day 17
- Advent of Code 2023 Day 18
- Advent of Code 2023 Day 19
- Advent of Code 2023 Day 20
- Advent of Code 2023 Day 21
- Advent of Code 2023 Day 22
- Advent of Code 2023 Day 23
- Advent of Code 2023 Day 25
-
Older post
-
Newer post
Advent of Code 2023 Day 6
Day 6: Wait For It
https://adventofcode.com/2023/day/6
A ferry brings you to a boat race. The prize for winning the race is a trip to desert island, you want to go there, because you heard some sand is needed to fix the snow issue. A island named Desert Island will surely have a lot of that annoying, course, stuff.
This race is slightly unusual, the amount of time everyone gets is fixed. Whoever gets the furthest in that time wins.
Today’s input is a report of previous races.
An example input looks like this:
It maps time-limits of races to the current furthest distance.
This document describes three races:
- Race 1 lasts 7 milliseconds. The record distance in this race is 9 millimeters.
- Race 2 lasts 15 milliseconds. The record distance in this race is 40 millimeters.
- Race 3 lasts 30 milliseconds. The record distance in this race is 200 millimeters.
The boats are toy boats.
Before they start moving, you have to charge them by holding a button. The longer you charge them, the larger the boat’s speed.
- The moment you start charging, the time starts
- The moment you stop charging, the boat starts moving (and doesn’t lose speed, apparently they were made by a physics teacher elf that assumed a frictionless vaccuum)
Part 1
For each race, determine the amount of ways you can beat the current record.
For the first race in the example (that lasts 7 milliseconds), there are 4 ways to beat the record of 9 millimeters:
- Hold the button for 2 milliseconds at the start of the race.
- Hold the button for 3 milliseconds at the start of the race.
- Hold the button for 4 milliseconds at the start of the race.
- Hold the button for 5 milliseconds at the start of the race.
The question asks you to multiply the number of ways you can win each race.
Parsing
Getting 2 lists of numbers, and combining them.
For my own convenience, I used a Race
structure to represent each race, so I don’t confuse myself by accessing things by index.
I chose to create a list of times and a list of distances, then zip those 2 lists together to get a list of races.
Code
The logic then loops over each race and determines how many ways I can win for each one. At the end, those counts are multiplied.
For every race: I determined the distance I would travel for every possible time I held the button. If the resulting final distance was greater than the current record, I won.
Part 2
It turns out the input had really bad keming. Each line is one number.
The question stays the same, but this time there is only 1 race.
Parsing
For both time, and distance, I made a list of single digits. For each digit I encountered, I concatenate it to the end of the current number with math.
In the example input:
For time:
- The first digit is 7, the current number becomes 7
- The second digit is 1, the current number becomes 71
- The third digit is 5, the current number becomes 715
- The fourth digit is 3, the current number becomes 7153
- The final digit is 0, the current number becomes 71530
Option 1: brute-force
The code from part 1 can largely be reused.
And joy, oh joy, I could even remove some code!
Code
Option 2: math
This question was describing a quadratic graph. The answer is the difference between the two intersections with the y-axis on that graph.
Time to dust of the highschool math!
The equation we used to determine the distance we travel:
Written differently, that looks more familiar:
To find the 2 intersections of the second degree function with the x-axis, we set y to 0 and solve for x:
A refresher on the quadratic equation from Khan Academy.
The answer is the difference between the two points where the graph intersects with the x-axis.
I can only hold the button for an integer amount, so I need to round up the lowest value, and round down the highest value.
Then add 1 to the difference between those 2, because I want to include that last point where I can win the race.