Refactor maps using defaultdict

This commit is contained in:
Jake Pullen
2023-12-05 07:43:29 +00:00
parent 68e84514da
commit 972d032360
+16 -15
View File
@@ -1,4 +1,4 @@
import os from collections import defaultdict
with open(r'advent_of_code\2023\day_5\input.txt', 'r') as file: with open(r'advent_of_code\2023\day_5\input.txt', 'r') as file:
input = file.readlines() input = file.readlines()
@@ -45,13 +45,14 @@ def build_instructions_map(instructions, instructions_map):
instructions_map[source] = destination instructions_map[source] = destination
# build the seed to soil map using the instructions from the input # build the seed to soil map using the instructions from the input
seed_to_soil_map = {} seed_to_soil_map = defaultdict(lambda: seed_to_soil_map)
soil_to_fertilizer_map = {} soil_to_fertilizer_map = defaultdict(lambda: soil_to_fertilizer_map)
fertilizer_to_water_map = {} fertilizer_to_water_map = defaultdict(lambda: fertilizer_to_water_map)
water_to_light_map = {} water_to_light_map = defaultdict(lambda: water_to_light_map)
light_to_temperature_map = {} light_to_temperature_map = defaultdict(lambda: light_to_temperature_map)
temperature_to_humidity_map = {} temperature_to_humidity_map = defaultdict(lambda: temperature_to_humidity_map)
humidity_to_location_map = {} humidity_to_location_map = defaultdict(lambda: humidity_to_location_map)
build_instructions_map(seed_to_soil_map_instructions, seed_to_soil_map) build_instructions_map(seed_to_soil_map_instructions, seed_to_soil_map)
build_instructions_map(soil_to_fertilizer_map_instructions, soil_to_fertilizer_map) build_instructions_map(soil_to_fertilizer_map_instructions, soil_to_fertilizer_map)
@@ -64,13 +65,13 @@ build_instructions_map(humidity_to_location_map_instructions, humidity_to_locati
locations = [] locations = []
# now we run the seed number through the maps 1 by 1 to get the final location # now we run the seed number through the maps 1 by 1 to get the final location
for seed_number in seed_numbers: for seed_number in seed_numbers:
soil = seed_to_soil_map.get(int(seed_number), int(seed_number)) soil = seed_to_soil_map[int(seed_number)]
fertilizer = soil_to_fertilizer_map.get(soil, soil) fertilizer = soil_to_fertilizer_map[soil]
water = fertilizer_to_water_map.get(fertilizer, fertilizer) water = fertilizer_to_water_map[fertilizer]
light = water_to_light_map.get(water, water) light = water_to_light_map[water]
temperature = light_to_temperature_map.get(light, light) temperature = light_to_temperature_map[light]
humidity = temperature_to_humidity_map.get(temperature, temperature) humidity = temperature_to_humidity_map[temperature]
location = humidity_to_location_map.get(humidity, humidity) location = humidity_to_location_map[humidity]
locations.append(location) locations.append(location)
#print lowest location #print lowest location