Skip to content

Instantly share code, notes, and snippets.

@d3v-null
Created December 5, 2023 06:30
Show Gist options
  • Save d3v-null/667c2f8ad17b90d36d818f1c9508016b to your computer and use it in GitHub Desktop.
Save d3v-null/667c2f8ad17b90d36d818f1c9508016b to your computer and use it in GitHub Desktop.
demonstration of how fast numpy operations can be used to speed up part 2 of aoc
import numpy as np
from more_itertools import chunked
map = [
[50, 98, 2],
[52, 50, 48]
]
def apply_map(map, srcs):
for dst_start, src_start, range_len in map:
srcs[np.where(np.logical_and(
src_start <= srcs,
srcs < src_start + range_len
))] += dst_start - src_start
if __name__ == '__main__':
seeds = [
79, 14,
55, 13
]
for seed_start, seed_range in chunked(seeds, 2):
locs = np.fromiter(range(seed_start, seed_start + seed_range), int)
apply_map(map, locs)
print(f"{seed_start=} {seed_range=} -> ", locs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment