DEV Community

Cover image for Advent of Code 2023 - December 21st
Rob van der Leek
Rob van der Leek

Posted on

Advent of Code 2023 - December 21st

In this series, I'll share my progress with the 2023 version of Advent of Code.

Check the first post for a short intro to this series.

You can also follow my progress on GitHub.

December 21st

The puzzle of day 21 is more of a math puzzle than it is a programming puzzle. The code is very straightforward but I needed a lot of hints for part two, which is basically solving a polynomial equation.

My pitfall for this puzzle: The second part would have taken me way too much time to figure it out on my own, therefore I needed to turn to Reddit for some hints.

Solution here, do not click if you want to solve the puzzle first yourself
#!/usr/bin/env python3

grid = []
with open('input.txt') as infile:
    lines = infile.readlines()
    for y, line in enumerate(lines):
        grid.append([c for c in line.strip()]) 
        if 'S' in line:
            S = (y, line.index('S'))

free_positions = set()
for y, row in enumerate(grid):
    for x, col in enumerate(row):
        if col == '.' or col == 'S':
            free_positions.add((y, x))

def show_grid(grid, positions):
    for y, row in enumerate(grid):
        line = '' 
        for x, col in enumerate(row):
            if (y, x) in positions:
                line += 'O'
            else:
                line += grid[y][x]
        print(line)

def free(pos):
   return (pos[0] % len(grid), pos[1] % len(grid[0])) in free_positions

def calc(n):
    positions = [S]
    for _ in range(n):
        new_positions = set()
        for p in positions:
            south = (p[0] + 1, p[1])
            if free(south) and not south in new_positions:
                new_positions.add(south)
            west = (p[0], p[1] - 1)
            if free(west) and not west in new_positions:
                new_positions.add(west)
            north = (p[0] - 1, p[1])
            if free(north) and not north in new_positions:
                new_positions.add(north)
            east = (p[0], p[1] + 1)
            if free(east) and not east in new_positions:
                new_positions.add(east)
        positions = new_positions
    return len(positions)

r1 = calc(65)
r2 = calc(65 + 131)
r3 = calc(65 + 2 * 131)
a = (r3 + r1 - (2 * r2)) / 2
b = ((4 * r2) - (3 * r1) - r3) / 2
c = r1
print((a * pow(202300, 2)) + (b * 202300) + c)
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up