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 15
Day 15: Lens Library
https://adventofcode.com/2023/day/15
You arrive at the facility the mirrors are pointing at.
Inside, some more calibration work is needed.
Today’s input is a bunch of instructions.
An example input looks like this:
The line with instructions is separated by commas.
Part 1
A hashing algorithm turns every instruction into a number.
- Determine the ASCII code for the current character of the string.
- Increase the current value by the ASCII code you just determined.
- Set the current value to itself multiplied by 17.
- Set the current value to the remainder of dividing itself by 256.
The question asks for the sum of all instruction hashes.
Helpers
The hash function!
By using some properties of modular arithmetic, and the fact that a 8 bytes can store 256 decimal numbers (0 to 255).
Code
Part 2
Now the question tells us what each instruction means.
Each instruction starts with a label (the letters).
The labels are for lenses.
There are 2 types of instruction:
- An instruction ending in a minus
-
means “remove the lens with this label from its box”. - An instructing with an equals sign
=
means “update the box with this lens”
To find the box for a label, apply the hashing function from part1 to it.
eg. The rn=1
instruction has a label of rn
, hashing that gives 0
.
So this instruction tells us to update box 0.
There are 256 boxes in total, this maps perfectly to what the hash function from part1 can return!
Now, the specifics of both instructions.
- The remove instruction:
- If the label you are searching for is not inside its box, do nothing.
- If the label you are searching for is present, remove it and shift all other lenses forward.
- The add instruction:
- If a lens with the label you are searching for is present, update its focal length (the number right of the
=
in the instruction). - If a lens with the label you are searching for is not present, add the lens to the end of the box.
- If a lens with the label you are searching for is present, update its focal length (the number right of the
The question asks for the total focusing power after all instructions are applied.
The focusing power of a single lens is the result of multiplying together:
- One plus the box number of the lens in question. (boxes start at 0!)
- The slot number of the lens within the box: 1 for the first lens, 2 for the second lens, and so on. (slots start 1!)
- The focal length of the lens.
Helpers
I represented each instruction as an enum.
A Lens
here:
Again, don’t mind the 'a
stuff, those are Rust lifetimes that let me use the labels from the input.
Then I created a way to turn a piece of the input into a real Instruction
.
I loop over the input, turn each instruction into an Instruction
and apply it.
I represented the 256 boxes as an array. Each box can hold several lenses, so each box is represented as a list itself.
For every instruction, I calculate the hash of its label.
Then I apply the instruction according to the rules described in the problem statement.
At the end, I loop through all boxes and calculate the sum of the focusing power. The sum of all those sums is what the question is asking for.