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 18
Day 18: Lavaduct Lagoon
https://adventofcode.com/2023/day/18
The factory needs so much lava to work through its backlog, that the elves are digging a collection pool right in front of the entrance.
Today’s input is a list of instructions to dig the perimeter of the pool.
An example input looks like this:
Each instruction tells the digger to move to a different point, and dig every tile it passes through.
When followed, it results in a closed loop of trench being dug.
Part 1
The first letter in an instruction is the direction the digger should move in next:
U
upD
downL
leftR
right
The number tells the digger how far it should move in that direction.
That hexcode looking thing is not used in part1.
Every tile the loop enloses should also be dug out.
The question asks how many tiles are dug out.
I broke this up into 2 parts:
- Parsing an instruction per line
- Calculating the area when following the instructions
Helpers
I represented each instruction as an Instr
:
Each direction is a Dir
:
The parsing:
If you follow each instruction, the trench is a polygon with square edges. The question is asking for the area of that polygon.
That polygon has a bunch of coordinates at its edges that are specified by following the instructions.
So I made a Coord
:
I used the shoelace formula to calculate its area.
More information about polygon coordinates and areas
Calculating the area, a function that takes the list of instructions:
That function uses a helper that lets a coordinate move in a direction for an amount of steps:
Code
Part 2
Oops! The instructions should be interpreted differently.
Each hexadecimal code is six digits long.
- The first five hexadecimal digits encode the distance in meters as a five-digit hexadecimal number.
- The last hexadecimal digit encodes the direction to dig: 0 means R, 1 means D, 2 means L, and 3 means U.
The question asks how many tiles are dug out.
Only the parsing logic changes.