Advertisement
illuminati229

AoC Day 11

Dec 11th, 2021
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def adj(loc):
  4.     adj_loc = []
  5.     for i in range(-1, 2):
  6.         for j in range(-1, 2):
  7.             if i == 0 and j == 0:
  8.                 pass
  9.             elif (loc[0] + i) < 0 or (loc[1] + j) < 0 or (loc[0] + i) > 9 or (loc[1] + j) > 9:
  10.                 pass
  11.             else:
  12.                 adj_loc.append([loc[0] + i, loc[1] + j])
  13.  
  14.     return adj_loc
  15.  
  16. def flash_count(file_path, sync):
  17.     o_map = np.genfromtxt(file_path, dtype = 'int', delimiter = 1)
  18.  
  19.     flash_count = 0
  20.  
  21.     for step in range(1, 1000):
  22.         o_map += 1
  23.         while o_map.max() > 9:
  24.             flash = np.argwhere(o_map > 9)
  25.             for loc in flash:
  26.                 o_map[loc[0],loc[1]] = 0
  27.                 for adj_loc in adj(loc):
  28.                     if o_map[adj_loc[0],adj_loc[1]] != 0:
  29.                         o_map[adj_loc[0],adj_loc[1]] += 1
  30.  
  31.         flash_count += np.count_nonzero(o_map == 0)
  32.        
  33.         if np.count_nonzero(o_map == 0) == 100:
  34.             return step
  35.        
  36.         if step == 100 and not sync:
  37.             return flash_count
  38.  
  39.  
  40. def main():
  41.    
  42.     assert flash_count('test_input.txt', False) == 1656
  43.     print(flash_count('input.txt', False))
  44.  
  45.     assert flash_count('test_input.txt', True) == 195
  46.     print(flash_count('input.txt', True))
  47.  
  48.  
  49. if __name__ == '__main__':
  50.     main()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement