Advertisement
hhoppe

Advent of code 2020 day 15

Dec 15th, 2020 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. # Accelerate the computation using numba.  This requires moving from a dict
  2. # to a large array.  It seems slower for small numbers, but is 15x faster
  3. # for the larger runs.
  4.  
  5. def process(s, num_turns=2020):
  6.   l = np.array(list(map(int, s.split(','))), dtype=np.int32)
  7.  
  8.   @numba.njit
  9.   def func():
  10.     last_turn = np.full(num_turns, -1, dtype=np.int32)
  11.     prev_turn = 0  # dummy initialization
  12.     for turn in range(1, num_turns + 1):
  13.       if turn <= len(l):
  14.         number = l[turn - 1]
  15.       elif prev_turn < 0:
  16.         number = 0
  17.       else:
  18.         number = last_turn[number] - prev_turn
  19.  
  20.       prev_turn = last_turn[number]
  21.       last_turn[number] = turn
  22.     return number
  23.  
  24.   return func()
  25.  
  26. hh.check_eq(process(puzzle_input, num_turns=30_000_000), 1407)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement