Advertisement
idoveolexvweplpwsd

Untitled

Dec 25th, 2021
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. import numpy as np
  2. import Inputs
  3.  
  4. def get_neighbor(val_map, index_tuple):
  5.     '''Gets the right or down element in the array.'''
  6.     row = index_tuple[0]
  7.     col = index_tuple[1]
  8.  
  9.     if val_map[row, col] == '>':
  10.         try:
  11.             return val_map[row, col+1]
  12.  
  13.         except IndexError:
  14.             return val_map[row, 0]
  15.  
  16.     elif val_map[row, col] == 'v':
  17.         try:
  18.             return val_map[row+1, col]
  19.         except IndexError:
  20.             return val_map[0, col]
  21.  
  22. def move(val_map, index_tuple):
  23.     '''Performs the moving action. Right for > and down for v'''
  24.     row = index_tuple[0]
  25.     col = index_tuple[1]
  26.    
  27.     if val_map[row, col] == '>':
  28.         try:
  29.             val_map[row, col+1] = '>'
  30.         except IndexError:
  31.             val_map[row, 0] = '>'
  32.  
  33.         val_map[row, col] = '.'
  34.  
  35.     else:
  36.         try:
  37.             val_map[row+1, col] = 'v'
  38.         except IndexError:
  39.             val_map[0, col] = 'v'
  40.  
  41.         val_map[row, col] = '.'
  42.        
  43.  
  44. puz_input = np.genfromtxt(r"2021\txtfiles\day25.txt", delimiter=1, dtype=np.str)
  45.  
  46. steps = 0
  47. while True:
  48.    
  49.     steps += 1
  50.     movements = 0
  51.  
  52.     indices_to_move = []
  53.  
  54.     # First check >. If able to move, add to list and then move all at once.
  55.     # If I moved them here, it would not account sea cucumbers that are on the edge correctly
  56.     for row, col in np.ndindex(puz_input.shape):
  57.         if puz_input[row, col] == '>':
  58.             neighbor = get_neighbor(puz_input, (row,col))
  59.  
  60.             if neighbor == '.':
  61.                 indices_to_move.append((row, col))
  62.  
  63.  
  64.     # Perform the moving action
  65.     if indices_to_move:
  66.         for idx in indices_to_move:
  67.             move(puz_input, idx)
  68.             movements += 1
  69.        
  70.     indices_to_move.clear()
  71.  
  72.     # Repeat the process with v sea cucumbers
  73.     for row, col in np.ndindex(puz_input.shape):
  74.         if puz_input[row, col] == 'v':
  75.             neighbor = get_neighbor(puz_input, (row,col))
  76.  
  77.             if neighbor == '.':
  78.                 indices_to_move.append((row, col))
  79.  
  80.  
  81.     if indices_to_move:
  82.         for idx in indices_to_move:
  83.             move(puz_input, idx)
  84.             movements += 1
  85.  
  86.  
  87.     # Once nothing moves, break the loop
  88.     if movements == 0:
  89.         print(f"Answer is: {steps}")
  90.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement