globmont

AoC 21-13

Dec 12th, 2021 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import numpy as np
  2. file = "inputs/2021/13/data.txt"
  3. with open(file, "r") as f:
  4.     sections = f.read().split("\n\n")
  5.    
  6.     coordinates = np.array([list(map(int, pair.split(","))) for pair in sections[0].split("\n")])
  7.     folds = sections[1].split("\n")
  8.    
  9.  
  10. coordinates = coordinates[:, ::-1]
  11. bounds = coordinates.max(axis=0) + 1
  12.  
  13. grid = np.zeros(bounds).astype(int)
  14. grid[coordinates[:, 0], coordinates[:, 1]] = 1
  15.  
  16. for idx, fold in enumerate(folds):
  17.     fold = fold.split(" ")[-1]
  18.     fold_direction = fold.split("=")[0]
  19.     fold_location = int(fold.split("=")[1])
  20.    
  21.     if fold_direction == "y":
  22.         # Fold up
  23.         fold_section = grid[fold_location + 1:, :][::-1]
  24.         base_section = grid[:fold_location, :]
  25.         grid = base_section
  26.         grid[-fold_section.shape[0]:, :] += fold_section
  27.     elif fold_direction == "x":
  28.         # Fold left
  29.         fold_section = grid[:, fold_location + 1:][:, ::-1]
  30.         base_section = grid[:, :fold_location]
  31.         grid = base_section
  32.         grid[:, -fold_section.shape[1]:] += fold_section
  33.    
  34.    
  35.     if idx == 0:
  36.         print(f"Part 1: {grid.astype(bool).sum().sum()}")
  37.     # break
  38.  
  39. np.set_printoptions(linewidth=10000)
  40. print("Part 2:")
  41. print(np.where(grid, 1, 0))
Add Comment
Please, Sign In to add comment