DEV Community

Cover image for Advent of Code 2023 - December 10th
Rob van der Leek
Rob van der Leek

Posted on • Edited on

Advent of Code 2023 - December 10th

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 10th

The puzzle of day 10 is a monster. It appears to be much easier than it is (it's a hair-puller). So far I've only solved the first part, and for that I needed to increase the recursion limit 😨 UPDATE: I've also solved part two.

My pitfall for this puzzle: I was convinced a backtrack solution was the way to go for this puzzle. However, the input does not work for the default Python recursion depth. Increasing the recursion depth limit feels bad, but I don't have the time to look into other ways to solve the puzzle. Part two also cost me a lot of time. This is not my kind of puzzle 😉

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

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

def get_pipe(y, x):
    if y < 0 or y >= len(grid) or x < 0 or x >= len(grid[y]):
        return None
    return grid[y][x]

visited = []

def walk(pos, trail):
    if pos in visited:
        if grid[pos[0]][pos[1]] == 'S' and len(trail) > 2:
            return trail
        else:
            return None
    else:
        visited.append(pos)
    result = None
    if get_pipe(pos[0], pos[1]) in ['-', 'L', 'F', 'S'] and \
            get_pipe(pos[0], pos[1] + 1) in ['-', 'J', '7', 'S']:
        result = walk((pos[0], pos[1] + 1), trail + [(pos[0], pos[1] + 1)])
    if not result and get_pipe(pos[0], pos[1]) in ['|', '7', 'F', 'S'] and \
            get_pipe(pos[0] + 1, pos[1]) in ['|', 'L', 'J', 'S']:
        result = walk((pos[0] + 1, pos[1]), trail + [(pos[0] + 1, pos[1])])
    if not result and get_pipe(pos[0], pos[1]) in ['-', 'J', '7', 'S'] and \
            get_pipe(pos[0], pos[1] - 1) in ['-', 'L', 'F', 'S']:
        result = walk((pos[0], pos[1] - 1), trail + [(pos[0], pos[1] - 1)])
    if not result and get_pipe(pos[0], pos[1]) in ['|', 'L', 'J', 'S'] and \
        get_pipe(pos[0] - 1, pos[1]) in ['|', '7', 'F', 'S']:
        result = walk((pos[0] - 1, pos[1]), trail + [(pos[0] - 1, pos[1])])
    return result

sys.setrecursionlimit(14000)
trail = walk(S, [])
print(math.floor(len(trail) / 2))

def replace_S(grid, trail):
    after = trail[0]
    before = trail[-2]
    if before[0] > S[0] and before[1] == S[1] and after[0] == S[0] and after[1] > S[1]:
        grid[S[0]][S[1]] = 'F'
    elif before[0] < S[0] and before[1] == S[1] and after[0] == S[0] and after[1] > S[1]:
        grid[S[0]][S[1]] = 'L'
    elif before[0] == S[0] and before[1] < S[1] and fter[0] > S[0] and after[1] == S[1]:
        grid[S[0]][S[1]] = '7'
    elif before[0] < S[0] and before[1] == S[1] and after[0] == S[0] and after[1] < S[1]:
        grid[S[0]][S[1]] = 'J'
    else:
        grid[S[0]][S[1]] = '|'

replace_S(grid, trail)

enclosed = 0
for idx_y, row in enumerate(grid):
    in_loop = in_l = in_f = False
    for idx_x, col in enumerate(grid[idx_y]):
        if (idx_y, idx_x) in trail:
            if in_f:
                if col == 'J':
                    in_loop = not in_loop
                    in_f = False
                elif col == '7':
                    in_f = False
            if in_l:
                if col == '7':
                    in_loop = not in_loop
                    in_l = False
                elif col == 'J':
                    in_l = False
            elif col in ['|']:
                in_loop = not in_loop
            elif col == 'L':
                in_l = True
            elif col == 'F':
                in_f = True
        else:
            if in_loop:
                enclosed += 1

print(enclosed)
Enter fullscreen mode Exit fullscreen mode

That's it! See you again tomorrow!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

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