Advent of Code 2023 - December 10th
Tags: advent, adventofcode, python, december
Published: 2023-12-12 10:07:36

---
title: Advent of Code 2023 - December 10th
published: true
description: My progress with the 2023 version of Advent of Code
tags: advent, adventofcode, python, december
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36qayk14ewje7rvykeg5.jpg
series: Advent of Code 2023
# published_at: 2023-12-01 17:41 +0000
---

In this series, I'll share my progress with the 2023 version of [Advent of Code](https://adventofcode.com/2023).

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

You can also follow my progress [on GitHub](https://github.com/robvanderleek/adventofcode).

## December 10th

[The puzzle of day 10](https://adventofcode.com/2023/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 :fearful: **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 :wink:

{% spoiler Solution here, do not click if you want to solve the puzzle first yourself %}

```python
#!/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)
```

{% endspoiler %}

That's it! See you again tomorrow!