Advertisement
illuminati229

AoC Day 13 Try 2

Dec 13th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def fold_count(file_path, first_fold_only):
  4.  
  5.     folds = []
  6.     points = []
  7.     with open(file_path) as fin:
  8.         points, folds = fin.read().strip().split('\n\n')
  9.         points = [(int(point.split(",")[0]), int(point.split(",")[1])) for point in points.splitlines()]
  10.         folds = [(axis[-1], int(val)) for fold in folds.splitlines() for axis, val in [fold.split("=")]]
  11.  
  12.     max_y = 0
  13.     max_x = 0
  14.  
  15.     for x, y in points:
  16.         if x > max_x:
  17.             max_x = x
  18.         if y > max_y:
  19.             max_y = y
  20.            
  21.     paper = np.zeros((max_x + 1,max_y + 1))
  22.  
  23.     for point in points:
  24.         paper[point] = 1
  25.  
  26.  
  27.    
  28.     for axis, loc in folds:
  29.         loc = int(loc)
  30.         if axis == 'x':
  31.             b = paper[(loc+1):,:]
  32.             b = np.flipud(b)
  33.             paper[(2*loc - len(paper)+1):loc,:] += b
  34.             paper = paper[:loc,:]
  35.         else:
  36.             b = paper[:,(loc+1):]
  37.             b = np.fliplr(b)
  38.             paper[:,(2*loc-len(paper[0])+1):loc] += b
  39.             paper = paper[:,:loc]
  40.  
  41.         if first_fold_only:
  42.             return np.count_nonzero(paper)
  43.    
  44.     paper = np.transpose(paper)
  45.    
  46.     for row in paper:
  47.         for item in row:
  48.             if item > 0:
  49.                 print('#',end='')
  50.             else:
  51.                 print(' ', end='')
  52.         print()
  53.  
  54.     return
  55.  
  56. def main():
  57.  
  58.     assert fold_count('test_input.txt', True) == 17
  59.     print(fold_count('input.txt',True))
  60.  
  61.     fold_count('test_input.txt', False)
  62.     fold_count('input.txt', False)
  63.  
  64.     fold_count('fun_input2.txt', False)
  65.  
  66.    
  67. if __name__ == '__main__':
  68.     main()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement