Advent of Code 2023 - December 14th
Tags: advent, adventofcode, python, december
Published: 2023-12-13 22:24:44

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

[The puzzle of day 14](https://adventofcode.com/2023/day/14) is quite fun and very doable. I probably could shave off more lines of code but as I'm still running behind with the other days I'm not going to spend more time on this puzzle.

__My pitfall for this puzzle:__ Used a lot of code, almost reaching the hard limit I set for myself at the start (100 lines.)

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

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

with open('input.txt') as infile:
    lines = infile.readlines()
    rows = []
    for line in lines:
        rows.append([c for c in line.strip()])
    platform = tuple(tuple(r) for r in rows)

def tilt_north(p):
    pl = [list(r) for r in p]
    while True:
        changed = False
        for idx, row in enumerate(pl):
            if idx <= len(pl) - 2:
                next_row = pl[idx + 1]
                for idx_x in range(len(row)):
                    if row[idx_x] == '.' and next_row[idx_x] == 'O':
                        row[idx_x] = 'O'
                        next_row[idx_x] = '.'
                        changed = True
        if not changed:
            break
    return tuple(tuple(r) for r in pl)

def tilt_south(p):
    pl = [list(r) for r in p]
    while True:
        changed = False
        for idx_y in range(len(pl) - 1, 0, -1):
            row = pl[idx_y]
            prev_row = pl[idx_y - 1]
            for idx_x in range(len(row)):
                if row[idx_x] == '.' and prev_row[idx_x] == 'O':
                    row[idx_x] = 'O'
                    prev_row[idx_x] = '.'
                    changed = True
        if not changed:
            break
    return tuple(tuple(r) for r in pl)

def tilt_east(p):
    pl = [list(r) for r in p]
    while True:
        changed = False
        for idx_y, row in enumerate(pl):
            for idx_x in range(len(row) - 1, 0, -1):
                if row[idx_x] == '.' and row[idx_x - 1] == 'O':
                    row[idx_x] = 'O'
                    row[idx_x - 1] = '.'
                    changed = True
        if not changed:
            break
    return tuple(tuple(r) for r in pl)

def tilt_west(p):
    pl = [list(r) for r in p]
    while True:
        changed = False
        for idx_y, row in enumerate(pl):
            for idx_x in range(len(row) - 1):
                if row[idx_x] == '.' and row[idx_x + 1] == 'O':
                    row[idx_x] = 'O'
                    row[idx_x + 1] = '.'
                    changed = True
        if not changed:
            break
    return tuple(tuple(r) for r in pl)

def calc_load(p):
    load = 0
    for idx, row in enumerate(p):
        load += (len(p) - idx) * len([c for c in row if c == 'O'])
    return load 

def cycle(p):
    return tilt_east(tilt_south(tilt_west(tilt_north(p))))

full_cycles = []
index = -1
cycle_index = -1
for i in range(1000000000):
    platform = cycle(platform)
    if platform in full_cycles:
        cycle_index = full_cycles.index(platform)
        index = i
        break
    full_cycles.append(platform)
cycles_left = (1000000000 - index) % (index - cycle_index)
for i in range(cycles_left - 1):
    platform = cycle(platform)
print(calc_load(platform))
```

{% endspoiler %}

That's it! See you again tomorrow!