Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

Advent of Code - 2023

This is a solution to Day 21 of Advent of Code 2023.

Day 21: Step Counter

You manage to catch the airship right as it's dropping someone else off on their all-expenses-paid trip to Desert Island! It even helpfully drops you off near the gardener and his massive farm.

"You got the sand flowing again! Great work! Now we just need to wait until we have enough sand to filter the water for Snow Island and we'll have snow again in no time."

While you wait, one of the Elves that works with the gardener heard how good you are at solving problems and would like your help. He needs to get his steps in for the day, and so he'd like to know which garden plots he can reach with exactly his remaining 64 steps.

He gives you an up-to-date map (your puzzle input) of his starting position (S), garden plots (.), and rocks (#). For example:

...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........

The Elf starts at the starting position (S) which also counts as a garden plot. Then, he can take one step north, south, east, or west, but only onto tiles that are garden plots. This would allow him to reach any of the tiles marked O:

...........
.....###.#.
.###.##..#.
..#.#...#..
....#O#....
.##.OS####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........

Then, he takes a second step. Since at this point he could be at either tile marked O, his second step would allow him to reach any garden plot that is one step north, south, east, or west of any tile that he could have reached after the first step:

...........
.....###.#.
.###.##..#.
..#.#O..#..
....#.#....
.##O.O####.
.##.O#...#.
.......##..
.##.#.####.
.##..##.##.
...........

After two steps, he could be at any of the tiles marked O above, including the starting position (either by going north-then-south or by going west-then-east).

A single third step leads to even more possibilities:

...........
.....###.#.
.###.##..#.
..#.#.O.#..
...O#O#....
.##.OS####.
.##O.#...#.
....O..##..
.##.#.####.
.##..##.##.
...........

He will continue like this until his steps for the day have been exhausted. After a total of 6 steps, he could reach any of the garden plots marked O:

...........
.....###.#.
.###.##.O#.
.O#O#O.O#..
O.O.#.#.O..
.##O.O####.
.##.O#O..#.
.O.O.O.##..
.##.#.####.
.##O.##.##.
...........

In this example, if the Elf's goal was to get exactly 6 more steps today, he could use them to reach any of 16 garden plots.

However, the Elf actually needs to get 64 steps today, and the map he's handed you is much larger than the example map.

Starting from the garden plot marked S on your map, how many garden plots could the Elf reach in exactly 64 steps?

Read input

A very familiar start for a puzzle: we read in a grid!

class Tile:
    START = 'S'
    GARDEN_PLOT = '.'
    ROCK = '#'
from utils import read_input

def initialize(example):
    grid = {}
    start = None
    for y, row in enumerate(read_input(21, example=example)):
        for x, cell in enumerate(row):
            grid[x+y*-1j] = cell
            if cell == Tile.START:
                start = x + y*-1j
                grid[x + y*-1j] = Tile.GARDEN_PLOT
    return grid, start
def get_neighbors(position):
    return [
        position + 1,
        position - 1,
        position + 1j,
        position - 1j
    ]

To walk through the garden, we keep track of the current start points and do steps amount of loops. For each step, we go through all of our current positions, get their neighbors, see which ones are garden plots (and thus, walkable) and mark those as the starting point for next round.

def take_steps(start, grid, steps):
    current = set([start])
    for _ in range(steps):
        next_positions = set()
        for position in current:
            potential_neighbors = get_neighbors(position)
            clear_neighbors = [
                n
                for n
                in potential_neighbors
                if grid[n] == Tile.GARDEN_PLOT
            ]
            next_positions.update(clear_neighbors)
        current = next_positions
    return current | set([start])

Let's take 64 steps and see where we end up in!

grid, start = initialize(example=False)

part_1 = len(take_steps(start, grid, 64))
print(f'Solution: {part_1}')
assert part_1 == 3762

Solution: 3762

Part 2

The Elf seems confused by your answer until he realizes his mistake: he was reading from a list of his favorite numbers that are both perfect squares and perfect cubes, not his step counter.

The actual number of steps he needs to get today is exactly 26501365.

He also points out that the garden plots and rocks are set up so that the map repeats infinitely in every direction.

So, if you were to look one additional map-width or map-height out from the edge of the example map above, you would find that it keeps repeating:

.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##...####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##..S####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##...####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................

This is just a tiny three-map-by-three-map slice of the inexplicably-infinite farm layout; garden plots and rocks repeat as far as you can see. The Elf still starts on the one middle tile marked S, though - every other repeated S is replaced with a normal garden plot (.).

Here are the number of reachable garden plots in this new infinite version of the example map for different numbers of steps:

  • In exactly 6 steps, he can still reach 16 garden plots.
  • In exactly 10 steps, he can reach any of 50 garden plots.
  • In exactly 50 steps, he can reach 1594 garden plots.
  • In exactly 100 steps, he can reach 6536 garden plots.
  • In exactly 500 steps, he can reach 167004 garden plots.
  • In exactly 1000 steps, he can reach 668697 garden plots.
  • In exactly 5000 steps, he can reach 16733044 garden plots.

However, the step count the Elf needs is much larger! Starting from the garden plot marked S on your infinite map, how many garden plots could the Elf reach in exactly 26501365 steps?

Since we're dealing with infinitely repeating grids, we need to first have a calculation in place to get the in-grid coordinate for any out-of-grid position.

Since we're using the Complex Number Coordinate System, it looks like this:

from functools import cache


@cache
def calculate_in_grid_position(n, width, height):
    x = int(n.real)
    y = int(n.imag)

    if x < 0 or x >= width:
        x = x % width
    if y > 0 or y <= (height * -1):
        y = (y*-1 % height) * -1

    return x+y * 1j

This infinite grid version of taking the steps is otherwise same as in part 1 but if we're not in the original grid, we calculate the matching in-grid coordinate and see if it's safe to step into.

def take_steps_2(start, grid, steps):
    width = int(max(c.real for c in grid)) + 1
    height = abs(int(min(c.imag for c in grid))) + 1
    current = set([start])

    for step in range(steps):
        next_positions = set()
        for position in current:
            potential_neighbors = get_neighbors(position)
            clear_neighbors = set()
            for n in potential_neighbors:
                if n in grid and grid[n] == Tile.GARDEN_PLOT:
                    clear_neighbors.add(n)
                elif n not in grid:
                    in_grid_n = calculate_in_grid_position(n, width, height)
                    if grid[in_grid_n] == Tile.GARDEN_PLOT:
                        clear_neighbors.add(n)
            next_positions.update(clear_neighbors)
        current = next_positions

    return current | set([start])

To keep iterating over my solution while improving its efficiency, I created this helper cell block that walks through the given example results to help me make sure I don't introduce any bugs.

On the first iteration, I managed to get up to 500 steps in short time, 500 in long time and 1000+ was already too much.

EXAMPLE = True
grid, start = initialize()
correct = [16, 50, 1594, 6536, 167004, 668697, 16733044]

for i, steps in enumerate([6, 10, 50, 100, 500, 1000, 5000]):
    print(f'=== {steps} ===')
    result = len(take_steps_2(start, grid, steps))
    assert result == correct[i], f"Mismatch: in {steps}: actual: {result} != expected: {correct[i]}"
    print('OK')

Then we could calculate the end result for this huge amount of steps (I wonder how long it would take for this elf to take this many steps...)

My current solution works but not efficient enough to get a result for such a huge number.

I tried a few things but after reading some discussions, this is apparently hard so I'm stopping here.

# part_2 = len(take_steps_2(start, grid, 26501365))
print(f'Solution: {part_2}')

One star closer to Christmas

Here we are, at 36 stars out of the possible 42 with 4 days to go. Realistically, I think I'll be able to hit 40 stars this year, maybe 42 if I revisit day 20 that I skipped completely.