Advent of Code 2023 - December 16th
Tags: advent, adventofcode, python, december
Published: 2023-12-18 23:10:17

---
title: Advent of Code 2023 - December 16th
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 16th

[The puzzle of day 16](https://adventofcode.com/2023/day/16) was a lot of instructions for a fairly trivial coding problem.

__My pitfall for this puzzle:__ The code is very inelegant, lots of `if` statements, and unfortunately no time to refactor this one.

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

```python
#!/usr/bin/env python3

with open('input-small.txt') as infile:
    lines = infile.readlines()

grid = []
for line in lines:
    grid.append([c for c in line.strip()])

def trace(beam, grid, visited):
    x = beam[1]
    y = beam[0]
    direction = beam[2]
    while True:
        if y < 0 or y >= len(grid) or x < 0 or x >= len(grid[0]):
            return []
        tile = grid[y][x]
        if (y, x, direction) in visited:
            return []
        else: 
            visited.append((y, x, direction))
        if tile == '|':
            if direction == 'left' or direction == 'right':
                return [(y - 1, x, 'up'), (y + 1, x, 'down')] 
        elif tile == '-':
            if direction == 'up' or direction == 'down':
                return [(y, x - 1, 'left'), (y, x + 1, 'right')]
        elif tile == '\\':
            if direction == 'up':
                return [(y, x - 1, 'left')]
            elif direction == 'down':
                return [(y, x + 1, 'right')]
            if direction == 'right':
                return [(y + 1, x, 'down')]
            elif direction == 'left':
                return [(y - 1, x, 'up')]
        elif tile == '/':
            if direction == 'up':
                return [(y, x + 1, 'right')]
            elif direction == 'down':
                return [(y, x - 1, 'left')]
            if direction == 'right':
                return [(y - 1, x, 'up')]
            elif direction == 'left':
                return [(y + 1, x, 'down')]
        if direction == 'right':
            x += 1
        elif direction == 'left':
            x -= 1
        elif direction == 'up':
            y -= 1
        elif direction == 'down':
            y += 1

def calc_energized(start, grid):
    beams = [start]
    visited = []
    while beams:
        beams.extend(trace(beams.pop(), grid, visited))
    visited = set([(v[0], v[1]) for v in visited])
    result = len(visited)
    print(f'{start} = {result}')
    return result

start_coords = []
start_coords.extend([(0, x, 'down') for x in range(len(grid[0]))])
start_coords.extend([(y, len(grid[0]) - 1, 'left') for y in range(len(grid))])
start_coords.extend([(len(grid) - 1, x, 'up') for x in range(len(grid[0]))])
start_coords.extend([(y, 0, 'right') for y in range(len(grid))])

print(max([calc_energized(s, grid) for s in start_coords]))
```

{% endspoiler %}

That's it! See you again tomorrow!