Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
-
Older post
-
Newer post
Advent of Code 2024 Day 7
Day 7: Bridge Repair
https://adventofcode.com/2024/day/7
Another day, another familiar location.
You are at a bridge that needs repairing.
The elves fixing it need to do some final calculations, but they lost their operators, elephants stole them!
Tricksy elephantses
An example input looks like this:
Each line is information for a calculation.
- The number to the left of the colon is the goal number, the number the calculation should result in.
- The numbers to the right of the colon are the operands, the numbers being operated on.
It is your job to determine if the goal can be reached by applying operators to the operands.
Operators are alway evaluated left to right, elves are not bound by puny things like order of operations when doing math.
The only types of operands that are missing is add: +
, and multiply *
.
Parsing
Again, keeping things simple because that’s the most surefire way lots of people can translate this code to their language of preference without much effort.
Take in the input, return a list of:
- The goal number
- A list of numbers
Part 1
The question asks for the sum of all goal numbers that are reachable.
Do you like recursion? I like recursion.
The main effort is in the helper function again.
In the part_1
code, I loop over each line, determine if it’s reachable, and add the goal number to a sum if it is.
Helper
The helper takes in a goal number and a list of operands. It returns a boolean stating if that goal is reachable given the list of nums.
It does so one number at a time, applying an operation (+
or *
), and recursing with the rest of the list.
I did this from left to right at first, but after watching HyperNeutrino’s video for today, I changed it to go from right to left.
This meant flipping the logic for the goal number from +
to -
, and from *
to /
.
But it allowed me to check if performing an operation would even make sense before recursing,
speeding the function up considerably.
Part 1 code
Part 2
Turn out the elephants also stole some concatenation operators.
It squooshes two numbers together real tight, it’s like string concatenation, but with numbers.
Helper
The helper function changes slightly. It adds a check to see if concatenating a number makes sense (if the goal number ends in the last number), then it recurses with that last number “un”-concatenated (is that a word? It is now!).
I did this with some math as this is much faster than converting the numbers to strings first.
Part 2 code
Same as part 1, but use the helper for part 2.
Final code
For the final code I combined both helpers into one, and changed the parsing logic to be able to operate on one line at a time without first parsing the entire input.