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 16
Day 16: The Floor Will Be Lava
https://adventofcode.com/2023/day/16
The light is focussed somewhere.
It goes into a big cavern with a bunch of mirrors and splitters in it.
Today’s input is a map of the cavern.
An example input looks like this:
.
empty space- ’\’ left mirror
- ’/’ right mirror
- ’-’ horizontal splitter
- ’|’ vertical splitter
If a beam hits an empty space, it keeps going like nothing happened (Because it didn’t you see? Nothing. It happened.)
If a beam hits a mirror, it is reflected 90 degrees according to the angle of the mirror it hit.
If a beam hits a splitter on the pointy end, it passes through like nothing happened.
If a beam hits a splitter on the flat end, it gets split and exits through the 2 pointy ends.
Beams do not interact, they pass right through eachother.
Parsing
I represent the cave as a 2D list of Tile
:
Part 1
The beam enters the cave from the top left, going right.
The question asks how many tiles are energized (1 or more beams pass through it).
Helpers
A Beam
struct represents a beam of light:
The position of a beam is stored as a Coord
:
And the direction a beam is currently travelling in as Direction
:
Some skeleton-code I want to work towards:
The main logic is in the energized
helper.
It keeps track of a set of energized coordinates. At the end, it returns how large that set is.
It starts by pushing a starting Beam
onto a queue.
Then it pops a beam off that queue until it’s empty. For each beam, it determines the next positions and directions, and adds those to the queue.
To prevent infinite loops (the beams might get trapped in the cave, going around forever), I keep track of every instance of Beam
I saw so far.
If I pop a beam off the stack I have already seen, I know I’m in a loop and can ignore that beam.
For every beam I pop off the queue, I determine the next direction(s) it will travel in: Either 1 direction if it reflects or keeps going, or 2 directions if the beam gets split.
For each direction, I try to move the beam in that direction. If it didn’t go off the sides of the map, I add a beam with that new direction and updated position to the queue.
The forward
helper that only returns a beam if it didn’t go off the edges of the map:
Code
Part 2
The beam can enter the cave from any position along the edges (heading directly away from that edge).
The question asks for the maximum amount of tiles that can be energized.
I brute forced this. I looped through every possible starting configuration, calculated the amount of energized tiles, and took the maximum.