Advent of Code 2023 – December 13th

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

The puzzle of day 13 is intimidating if you read the description but in the end not so hard.

My pitfall for this puzzle: Had to use a lot of code and did not have time for anything fancy.

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

#!/usr/bin/env python3 from copy import deepcopy

with open('input.txt') as infile:
    lines = [line.strip() for line in infile.readlines()]

patterns = []
cur_pattern = []
for line in lines:
    if line == '':
        patterns.append(cur_pattern)
        cur_pattern = []
    else:
        cur_pattern.append([c for c in line])
patterns.append(cur_pattern)

def find_vertical(p, exclude = None):
    for idx in range(1, len(p[0])):
        length = min(idx, len(p[0]) - idx)
        left = []
        for col in range(idx - length, idx):
            left.append(''.join([row[col] for row in p])) 
        right = []
        for col in range(idx, idx + length):
            right.append(''.join([row[col] for row in p]))
        if left == right[::-1] and idx != exclude:
            return idx
    return None

def find_horizontal(p, exclude = None):
    for idx in range(1, len(p)):
        length = min(idx, len(p) - idx)
        up = p[idx - length:idx]
        down = p[idx:idx + length]
        if up == down[::-1] and idx != exclude:
            return idx
    return None

def fix_smudge(p):
    result = []
    for idx_y in range(len(p)):
        for idx_x in range(len(p[0])):
            p_copy = deepcopy(p)
            cur = p_copy[idx_y][idx_x]
            if cur == '.':
                p_copy[idx_y][idx_x] = '#'
            else:
                p_copy[idx_y][idx_x] = '.'
            result.append(p_copy)
    return result

def find_reflection(p, exclude=None):
    n = find_vertical(p) if not exclude or exclude >= 100 \
        else find_vertical(p, exclude)
    if n:
        return n
    else:
        n = find_horizontal(p) if not exclude or exclude < 100 else \
            find_horizontal(p, exclude / 100) 
        if n:
            return n * 100

total = 0
for p in patterns:
    ref_p = find_reflection(p)
    fixed = fix_smudge(p)
    for f in fixed:
        ref_f = find_reflection(f, ref_p)
        if ref_f:
            total += ref_f
            break
print(total)

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

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

请登录后发表评论

    暂无评论内容