Created
December 5, 2023 06:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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