Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Accelerate the computation using numba. This requires moving from a dict
- # to a large array. It seems slower for small numbers, but is 15x faster
- # for the larger runs.
- def process(s, num_turns=2020):
- l = np.array(list(map(int, s.split(','))), dtype=np.int32)
- @numba.njit
- def func():
- last_turn = np.full(num_turns, -1, dtype=np.int32)
- prev_turn = 0 # dummy initialization
- for turn in range(1, num_turns + 1):
- if turn <= len(l):
- number = l[turn - 1]
- elif prev_turn < 0:
- number = 0
- else:
- number = last_turn[number] - prev_turn
- prev_turn = last_turn[number]
- last_turn[number] = turn
- return number
- return func()
- hh.check_eq(process(puzzle_input, num_turns=30_000_000), 1407)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement