Skip to content

Instantly share code, notes, and snippets.

@Mu-adventofcode
Created May 6, 2022 18:08
Show Gist options
  • Save Mu-adventofcode/7a06ebbf52bd864bd759d36e1721eb50 to your computer and use it in GitHub Desktop.
Save Mu-adventofcode/7a06ebbf52bd864bd759d36e1721eb50 to your computer and use it in GitHub Desktop.
Advent of Code 2021 day 13
"""
My solution for Advent of Code 2021 day 13 parts 1 and 2.
"""
import numpy as np
import re
# parse input
with open("input_13.txt") as f:
data = f.read()
dots = {(int(y), int(x)) for x, y in re.findall(r"(\d+),(\d+)", data)}
instructions = [(d, int(num)) for d, num in re.findall(r"([xy])=(\d+)", data)]
# prepare paper
ymax = max(y for y, _ in dots)
xmax = max(x for _, x in dots)
paper = np.zeros((3 + ymax, 1 + xmax)) # 3 was found by trial and error
for dot in dots:
paper[dot] = 1
# follow folding instructions
for i, (d, num) in enumerate(instructions):
if d == "x":
paper = paper.transpose()
flipped = np.flipud(paper)
combined = np.array(paper + flipped > 0)
paper = np.resize(combined, (num, paper.shape[1]))
if d == "x":
paper = paper.transpose()
if i == 0:
print("part 1:", np.sum(paper > 0))
#
print("part 2:")
for line in paper:
print("".join("█" if b else " " for b in line))
@Mu-adventofcode
Copy link
Author

A much better solution here https://github.com/fogleman/AdventOfCode2021/blob/main/13.py
I took its input parsing method. Thanks @fogleman.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment