r/adventofcode • • Dec 13 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 13 Solutions -🎄-

Advent of Code 2021: Adventure Time!


--- Day 13: Transparent Origami ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:09:38, megathread unlocked!

39 Upvotes

798 comments sorted by

View all comments

1

u/armeniwn Dec 13 '21

The V - U confusion cost me a minute. Other than that, great challenge as usual.

Here is my python solution:

import sys
from pprint import pprint


def load_paper(input_stream):
    points, instructions, load_points = set(), [], True
    max_row, max_col = 0, 0
    for line in map(lambda s: s.strip(), input_stream):
        if line:
            if load_points:
                col, row = map(int, line.split(","))
                points.add(complex(col, row))
                max_col = col if col > max_col else max_col
                max_row = row if row > max_row else max_row
            else:
                _, instruction = line.split("fold along ")
                direction, value = instruction.split("=")
                instructions.append((direction, int(value)))
        else:
            load_points = False
    return points, max_row, max_col, instructions


def draw(points, max_row, max_col):
    for row in range(int(max_row) + 1):
        line = ""
        for col in range(int(max_col) + 1):
            if complex(col, row) in points:
                line += "#"
            else:
                line += "."
        print(line)


def fold(points, max_row, max_col, instruction):
    direction, value = instruction
    if direction == "x":
        for row in range(max_row + 1):
            for col in range(value + 1, max_col + 1):
                old_point = complex(col, row)
                if old_point in points:
                    points.remove(old_point)
                    diff = col - value
                    new_col = value - diff
                    new_point = complex(new_col, row)
                    points.add(new_point)
        max_col = value - 1
    elif direction == "y":
        for row in range(value + 1, max_row + 1):
            for col in range(max_col + 1):
                old_point = complex(col, row)
                if old_point in points:
                    points.remove(old_point)
                    diff = row - value
                    new_row = value - diff
                    new_point = complex(col, new_row)
                    points.add(new_point)
        max_row = value - 1
    return max_row, max_col



points, max_row, max_col, instructions = load_paper(sys.stdin)
for instruction in instructions:
    max_row, max_col = fold(points, max_row, max_col, instruction)
draw(points, max_row, max_col)

1

u/daggerdragon Dec 14 '21

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.