Advertisement
illuminati229

AoC 2022 Day 12

Dec 12th, 2022 (edited)
2,307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. import numpy as np
  2. import networkx as nx
  3. from time import time
  4.  
  5.  
  6. def timer_func(func):
  7.     # This function shows the execution time of
  8.     # the function object passed
  9.     def wrap_func(*args, **kwargs):
  10.         t1 = time()
  11.         result = func(*args, **kwargs)
  12.         t2 = time()
  13.         print(f'Function {func.__name__!r} executed in {(t2 - t1):.4f}s')
  14.         return result
  15.  
  16.     return wrap_func
  17.  
  18.  
  19. def up_val(ix, iy, alt_map):
  20.     if ord(alt_map[ix - 1, iy]) - ord(alt_map[ix, iy]) > 1:
  21.         return 0
  22.     else:
  23.         return 1
  24.  
  25.  
  26. def down_val(ix, iy, alt_map):
  27.     if ord(alt_map[ix + 1, iy]) - ord(alt_map[ix, iy]) > 1:
  28.         return 0
  29.     else:
  30.         return 1
  31.  
  32.  
  33. def left_val(ix, iy, alt_map):
  34.     if ord(alt_map[ix, iy - 1]) - ord(alt_map[ix, iy]) > 1:
  35.         return 0
  36.     else:
  37.         return 1
  38.  
  39.  
  40. def right_val(ix, iy, alt_map):
  41.     if ord(alt_map[ix, iy + 1]) - ord(alt_map[ix, iy]) > 1:
  42.         return 0
  43.     else:
  44.         return 1
  45.  
  46.  
  47. @timer_func
  48. def day12(filepath, if_any=False):
  49.     with open(filepath) as fin:
  50.         alt_map = np.genfromtxt(filepath, delimiter=1, dtype=str)
  51.  
  52.     start = tuple(np.argwhere(alt_map == 'S')[0])
  53.     end = tuple(np.argwhere(alt_map == 'E')[0])
  54.     alt_map[start] = 'a'
  55.     alt_map[end] = 'z'
  56.  
  57.     G = nx.DiGraph()
  58.  
  59.     for ix, iy in np.ndindex(alt_map.shape):
  60.         if ix > 0:
  61.             if up_val(ix, iy, alt_map):
  62.                 G.add_edge((ix, iy), (ix - 1, iy))
  63.         if ix < alt_map.shape[0] - 1:
  64.             if down_val(ix, iy, alt_map):
  65.                 G.add_edge((ix, iy), (ix + 1, iy))
  66.         if iy > 0:
  67.             if left_val(ix, iy, alt_map):
  68.                 G.add_edge((ix, iy), (ix, iy - 1))
  69.         if iy < alt_map.shape[1] - 1:
  70.             if right_val(ix, iy, alt_map):
  71.                 G.add_edge((ix, iy), (ix, iy + 1))
  72.  
  73.     if not if_any:
  74.         steps = nx.dijkstra_path_length(G, start, end)
  75.     else:
  76.         starts = [tuple(x) for x in np.argwhere(alt_map == 'a')]
  77.         results = nx.multi_source_dijkstra(G, starts, end)
  78.         steps = results[0]
  79.  
  80.     return steps
  81.  
  82.  
  83. def main():
  84.     assert day12('test12') == 31
  85.     print(day12('input12'))
  86.  
  87.     assert day12('test12', True) == 29
  88.     print(day12('input12', True))
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement