Advent of Code 2023 – December 14th

Advent of Code 2023 (25 Part Series)

1 Advent of Code 2023
2 Advent of Code 2023 – December 2nd
21 more parts…
3 Advent of Code 2023 – December 3rd
4 Advent of Code 2023 – December 4th
5 Advent of Code 2023 – December 5th
6 Advent of Code 2023 – December 6th
7 Advent of Code 2023 – December 7th
8 Advent of Code 2023 – December 8th
9 Advent of Code 2023 – December 9th
10 Advent of Code 2023 – December 10th
11 Advent of Code 2023 – December 11th
12 Advent of Code 2023 – December 12th
13 Advent of Code 2023 – December 13th
14 Advent of Code 2023 – December 14th
15 Advent of Code 2023 – December 15th
16 Advent of Code 2023 – December 16th
17 Advent of Code 2023 – December 17th
18 Advent of Code 2023 – December 18th
19 Advent of Code 2023 – December 19th
20 Advent of Code 2023 – December 20th
21 Advent of Code 2023 – December 21st
22 Advent of Code 2023 – December 22nd
23 Advent of Code 2023 – December 23rd
24 Advent of Code 2023 – December 24th
25 Advent of Code 2023 – December 25th

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

The puzzle of 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.)

Solution here, do not click if you want to solve the puzzle first yourself

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

Enter fullscreen mode Exit fullscreen mode

That’s it! See you again tomorrow!

Advent of Code 2023 (25 Part Series)

1 Advent of Code 2023
2 Advent of Code 2023 – December 2nd
21 more parts…
3 Advent of Code 2023 – December 3rd
4 Advent of Code 2023 – December 4th
5 Advent of Code 2023 – December 5th
6 Advent of Code 2023 – December 6th
7 Advent of Code 2023 – December 7th
8 Advent of Code 2023 – December 8th
9 Advent of Code 2023 – December 9th
10 Advent of Code 2023 – December 10th
11 Advent of Code 2023 – December 11th
12 Advent of Code 2023 – December 12th
13 Advent of Code 2023 – December 13th
14 Advent of Code 2023 – December 14th
15 Advent of Code 2023 – December 15th
16 Advent of Code 2023 – December 16th
17 Advent of Code 2023 – December 17th
18 Advent of Code 2023 – December 18th
19 Advent of Code 2023 – December 19th
20 Advent of Code 2023 – December 20th
21 Advent of Code 2023 – December 21st
22 Advent of Code 2023 – December 22nd
23 Advent of Code 2023 – December 23rd
24 Advent of Code 2023 – December 24th
25 Advent of Code 2023 – December 25th

原文链接:Advent of Code 2023 – December 14th

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容