My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update solution for 2023/05

Add multi-threaded option (haha cpu go brrrrr)

+17 -5
+17 -5
2023/day05.py
··· 1 1 import fileinput 2 2 import itertools 3 + import multiprocessing 3 4 4 5 5 6 def seed_to_location(mappings, seed): ··· 47 48 print("Part 1:", min(seed_to_location(mappings, seed) for seed in seeds)) 48 49 49 50 # Solve Part 2. 51 + MULTITHREAD = True 52 + NUM_THREADS = multiprocessing.cpu_count() 53 + 54 + def worker(offset, step=NUM_THREADS): 55 + for location in itertools.count(start=(1 + offset), step=step): 56 + seed = location_to_seed(mappings, location) 57 + if seed_in_start_range(seed, seed_starts, seed_ranges): 58 + return location 59 + 60 + 50 61 seed_starts = seeds[::2] 51 62 seed_ranges = seeds[1::2] 52 63 53 - for location in itertools.count(start=1): 54 - seed = location_to_seed(mappings, location) 55 - if seed_in_start_range(seed, seed_starts, seed_ranges): 56 - break 64 + if MULTITHREAD: 65 + with multiprocessing.Pool(processes=NUM_THREADS) as pool: 66 + results = pool.map(worker, range(NUM_THREADS)) 67 + else: 68 + results = [worker(0, step=1)] 57 69 58 - print("Part 2:", location) 70 + print("Part 2:", min(results)) 59 71