stopped trying to store the mappings,

ate the memory like cookies.
instead we work out the kay:value on the fly
This commit is contained in:
Jake Pullen
2023-12-05 07:51:36 +00:00
parent 972d032360
commit 811d11818f
+29 -22
View File
@@ -33,6 +33,14 @@ light_to_temperature_map_instructions = input[line_light_to_temperature_map+1:li
temperature_to_humidity_map_instructions = input[line_temperature_to_humidity_map+1:line_humidity_to_location_map] temperature_to_humidity_map_instructions = input[line_temperature_to_humidity_map+1:line_humidity_to_location_map]
humidity_to_location_map_instructions = input[line_humidity_to_location_map+1:] humidity_to_location_map_instructions = input[line_humidity_to_location_map+1:]
def map_value(value, instructions):
for instruction in instructions:
instruction = instruction.strip() # remove newline character
if instruction: # check if instruction is not empty
destination_start, source_start, length = map(int, instruction.split())
if source_start <= value < source_start + length:
return destination_start + (value - source_start)
return value # if value is not in any range, it maps to itself
def build_instructions_map(instructions, instructions_map): def build_instructions_map(instructions, instructions_map):
for instruction in instructions: for instruction in instructions:
@@ -45,33 +53,32 @@ 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 = defaultdict(lambda: seed_to_soil_map) # seed_to_soil_map = defaultdict(lambda: seed_to_soil_map)
soil_to_fertilizer_map = defaultdict(lambda: soil_to_fertilizer_map) # soil_to_fertilizer_map = defaultdict(lambda: soil_to_fertilizer_map)
fertilizer_to_water_map = defaultdict(lambda: fertilizer_to_water_map) # fertilizer_to_water_map = defaultdict(lambda: fertilizer_to_water_map)
water_to_light_map = defaultdict(lambda: water_to_light_map) # water_to_light_map = defaultdict(lambda: water_to_light_map)
light_to_temperature_map = defaultdict(lambda: light_to_temperature_map) # light_to_temperature_map = defaultdict(lambda: light_to_temperature_map)
temperature_to_humidity_map = defaultdict(lambda: temperature_to_humidity_map) # temperature_to_humidity_map = defaultdict(lambda: temperature_to_humidity_map)
humidity_to_location_map = defaultdict(lambda: 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)
build_instructions_map(fertilizer_to_water_map_instructions, fertilizer_to_water_map) # build_instructions_map(fertilizer_to_water_map_instructions, fertilizer_to_water_map)
build_instructions_map(water_to_light_map_instructions, water_to_light_map) # build_instructions_map(water_to_light_map_instructions, water_to_light_map)
build_instructions_map(light_to_temperature_map_instructions, light_to_temperature_map) # build_instructions_map(light_to_temperature_map_instructions, light_to_temperature_map)
build_instructions_map(temperature_to_humidity_map_instructions, temperature_to_humidity_map) # build_instructions_map(temperature_to_humidity_map_instructions, temperature_to_humidity_map)
build_instructions_map(humidity_to_location_map_instructions, humidity_to_location_map) # build_instructions_map(humidity_to_location_map_instructions, humidity_to_location_map)
locations = [] locations = []
# 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[int(seed_number)] soil = map_value(int(seed_number), seed_to_soil_map_instructions)
fertilizer = soil_to_fertilizer_map[soil] fertilizer = map_value(soil, soil_to_fertilizer_map_instructions)
water = fertilizer_to_water_map[fertilizer] water = map_value(fertilizer, fertilizer_to_water_map_instructions)
light = water_to_light_map[water] light = map_value(water, water_to_light_map_instructions)
temperature = light_to_temperature_map[light] temperature = map_value(light, light_to_temperature_map_instructions)
humidity = temperature_to_humidity_map[temperature] humidity = map_value(temperature, temperature_to_humidity_map_instructions)
location = humidity_to_location_map[humidity] location = map_value(humidity, humidity_to_location_map_instructions)
locations.append(location) locations.append(location)
#print lowest location #print lowest location