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 23
Day 23: A Long Walk
https://adventofcode.com/2023/day/23
The water is flowing, you have some downtime and go for a walk.
A long walk, as long as possible!
Today’s input is a map of the surrounding area you are going to take a walk in.
An example input looks like this:
-
#
is a forest -
.
is a path -
^
is a slope going up -
v
is a slope going down -
<
is a slope going left -
>
is a slope going right -
You start at the open position in the top left.
-
You end in the open position in the bottom right.
You never visit the same spot twice during the walk.
Parsing
A grid problem you say? With coordinates? Uh-huh.
And the tiles:
A function returning the coordinates of the starting position, the ending position, and a 2D grid of Tile
:
Part 1
The slopes are slippery, you can only cross them in the direction they are pointing.
The question asks how many steps the longest hike is.
Helpers
A function that returns the neighbouring coordinates for a position, but only if I’m allowed to go there. Again, I opted for verbosity because this was nice and quick to code:
Code
Now BFS every possible path, while keeping track of the maximum cost when I land on the ending tile.
Part 2
The slopes aren’t slippery at all, and these boots are made for walking. You can now cross slopes in any direction.
The question asks how many steps the longest hike is.
I first tried doing the logic thing, changing the logic for neighbours
and seeing what happens.
What happens is my pc freezes.
The solution I’m working towards is the following: condense the map so each single-path chain is condensed into 1 point.
in pseudo/skeleton-code:
Helpers
I’ll need that neighbours logic anyway, so here it is. I used a different technique now because I felt like it, but the result is the same, a list of adjacent tiles I am allowed to step on:
A function to gather all points in the map where we can go more than a single direction:
A function to calculate the cost of going from 1 point to a different point. Make sure to track where I’ve been while reaching that point, to prevent going to impossible to reach points (backtracking is forbidden):
And a function that uses that costmap to calculate the longest path possible from one coordinate to an other:
Putting everything together means a few function calls, and final code that closely resembles that skeleton.