Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- file = "inputs/2021/13/data.txt"
- with open(file, "r") as f:
- sections = f.read().split("\n\n")
- coordinates = np.array([list(map(int, pair.split(","))) for pair in sections[0].split("\n")])
- folds = sections[1].split("\n")
- coordinates = coordinates[:, ::-1]
- bounds = coordinates.max(axis=0) + 1
- grid = np.zeros(bounds).astype(int)
- grid[coordinates[:, 0], coordinates[:, 1]] = 1
- for idx, fold in enumerate(folds):
- fold = fold.split(" ")[-1]
- fold_direction = fold.split("=")[0]
- fold_location = int(fold.split("=")[1])
- if fold_direction == "y":
- # Fold up
- fold_section = grid[fold_location + 1:, :][::-1]
- base_section = grid[:fold_location, :]
- grid = base_section
- grid[-fold_section.shape[0]:, :] += fold_section
- elif fold_direction == "x":
- # Fold left
- fold_section = grid[:, fold_location + 1:][:, ::-1]
- base_section = grid[:, :fold_location]
- grid = base_section
- grid[:, -fold_section.shape[1]:] += fold_section
- if idx == 0:
- print(f"Part 1: {grid.astype(bool).sum().sum()}")
- # break
- np.set_printoptions(linewidth=10000)
- print("Part 2:")
- print(np.where(grid, 1, 0))
Add Comment
Please, Sign In to add comment