First solution to part2
It's kind of a time hog, though
This commit is contained in:
parent
e46a2717b4
commit
b588eeb7e5
@ -30,7 +30,7 @@ def extract(input_file: str) -> Tuple[List[int], Dict[str, List[AlmanacMap]]]:
|
||||
"humidity-to-location": [],
|
||||
}
|
||||
|
||||
with open("input.txt") as input:
|
||||
with open(input_file) as input:
|
||||
current_map = {}
|
||||
while line := input.readline():
|
||||
if match := re.match("seeds: (.*)", line):
|
||||
|
@ -10,7 +10,7 @@ for seed in seeds:
|
||||
soil_maps = list(filter(lambda soil_map: soil_map.is_mapped(seed), maps["seed-to-soil"]))
|
||||
soil = seed if soil_maps == [] else soil_maps[0].mapped_value(seed)
|
||||
fertilizer_maps = list(filter(lambda fertilizer_map: fertilizer_map.is_mapped(soil), maps["soil-to-fertilizer"]))
|
||||
print(fertilizer_maps)
|
||||
#print(fertilizer_maps)
|
||||
fertilizer = soil if fertilizer_maps == [] else fertilizer_maps[0].mapped_value(soil)
|
||||
water_maps = list(filter(lambda water_map: water_map.is_mapped(fertilizer), maps["fertilizer-to-water"]))
|
||||
water = fertilizer if water_maps == [] else water_maps[0].mapped_value(fertilizer)
|
||||
|
46
day5/part2_time_hog.py
Executable file
46
day5/part2_time_hog.py
Executable file
@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3.11
|
||||
# Is killed on WSL2 after vmmem increase to 16GB
|
||||
from data import extract
|
||||
import concurrent.futures
|
||||
|
||||
def seed_to_location(seed, maps):
|
||||
print(seed)
|
||||
soil_maps = list(filter(lambda soil_map: soil_map.is_mapped(seed), maps["seed-to-soil"]))
|
||||
soil = seed if soil_maps == [] else soil_maps[0].mapped_value(seed)
|
||||
fertilizer_maps = list(filter(lambda fertilizer_map: fertilizer_map.is_mapped(soil), maps["soil-to-fertilizer"]))
|
||||
#print(fertilizer_maps)
|
||||
fertilizer = soil if fertilizer_maps == [] else fertilizer_maps[0].mapped_value(soil)
|
||||
water_maps = list(filter(lambda water_map: water_map.is_mapped(fertilizer), maps["fertilizer-to-water"]))
|
||||
water = fertilizer if water_maps == [] else water_maps[0].mapped_value(fertilizer)
|
||||
light_maps = list(filter(lambda light_map: light_map.is_mapped(water), maps["water-to-light"]))
|
||||
light = water if light_maps == [] else light_maps[0].mapped_value(water)
|
||||
temperature_maps = list(filter(lambda temperature_map: temperature_map.is_mapped(light), maps["light-to-temperature"]))
|
||||
temperature = light if temperature_maps == [] else temperature_maps[0].mapped_value(light)
|
||||
humidity_maps = list(filter(lambda humidity_map: humidity_map.is_mapped(temperature), maps["temperature-to-humidity"]))
|
||||
humidity = temperature if humidity_maps == [] else humidity_maps[0].mapped_value(temperature)
|
||||
location_maps = list(filter(lambda location_map: location_map.is_mapped(humidity), maps["humidity-to-location"]))
|
||||
location = humidity if location_maps == [] else location_maps[0].mapped_value(humidity)
|
||||
return location
|
||||
|
||||
def parallel_loop(seeds, maps, index):
|
||||
lowest_location = None
|
||||
for seed_idx in range(seeds[index]):
|
||||
seed = seeds[index] + seed_idx
|
||||
location = seed_to_location(seed, maps)
|
||||
if lowest_location is not None:
|
||||
lowest_location = min(lowest_location, location)
|
||||
else:
|
||||
lowest_location = location
|
||||
seeds, maps = extract("input.txt")
|
||||
print(seeds)
|
||||
|
||||
|
||||
minimums = []
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
||||
for idx in range(0, len(seeds), 2):
|
||||
minimum = executor.submit(parallel_loop, seeds, maps, idx)
|
||||
minimums.append(minimum)
|
||||
|
||||
|
||||
|
||||
print(min(minimums))
|
Loading…
x
Reference in New Issue
Block a user