Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Using numba with doubly-linked list approach adapted from
- # https://github.com/shaeberling/euler/blob/master/kotlin/src/com/s13g/aoc/aoc2022/Day20.kt
- @numba.njit
- def day20_mix(a):
- for i in range(len(a)): # pylint: disable=consider-using-enumerate
- value, prev_, next_ = a[i]
- offset = value % (len(a) - 1)
- if offset != 0:
- a[next_, 1], a[prev_, 2] = prev_, next_ # Disconnect node i.
- prev_ = i
- if offset < len(a) // 2:
- for _ in range(offset):
- prev_ = a[prev_, 2]
- else:
- for _ in range(len(a) - offset):
- prev_ = a[prev_, 1]
- next_ = a[prev_, 2]
- a[i, 1], a[i, 2], a[prev_, 2], a[next_, 1] = prev_, next_, i, i # Reconnect node i.
- def day20(s, *, part2=False):
- a = np.array(list(map(int, s.splitlines())))
- a *= 811_589_153 if part2 else 1
- a = np.array([[value, (i - 1) % len(a), (i + 1) % len(a)] for i, value in enumerate(a)])
- for _ in range(10 if part2 else 1):
- day20_mix(a)
- (i,), = np.nonzero(a[:, 0] == 0)
- values = []
- for _ in range(len(a)):
- values.append(a[i, 0])
- i = a[i, 2]
- return sum(values[offset % len(a)] for offset in [1000, 2000, 3000])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement