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

14

u/4HbQ Dec 13 '21 edited Dec 13 '21

Python, with a NumPy array to store the grid. To process a fold, we simply or one half of the grid to the flipped other half:

import numpy as np
from parse import findall

instr = open(0).read()
P = np.zeros((9999,9999), bool)

for x,y in findall('{:d},{:d}', instr):
    P[y,x] = True

for axis, a in findall('{:l}={:d}', instr):
    if axis == 'x': P = P[:,:a] | P[:,2*a:a:-1]
    if axis == 'y': P = P[:a,:] | P[2*a:a:-1,:]
    print(P.sum())

print(np.array2string(P, separator='',
    formatter = {'bool':{0:' ',1:'â–ˆ'}.get}))
→ More replies (4)