From 811d11818fda5fa0c56610492fda76e756abf376 Mon Sep 17 00:00:00 2001 From: Jake Pullen Date: Tue, 5 Dec 2023 07:51:36 +0000 Subject: [PATCH] stopped trying to store the mappings, ate the memory like cookies. instead we work out the kay:value on the fly --- 2023/day_5/part 1 solution.py | 51 ++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/2023/day_5/part 1 solution.py b/2023/day_5/part 1 solution.py index a758404..7624405 100644 --- a/2023/day_5/part 1 solution.py +++ b/2023/day_5/part 1 solution.py @@ -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] 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): for instruction in instructions: @@ -45,33 +53,32 @@ def build_instructions_map(instructions, instructions_map): instructions_map[source] = destination # build the seed to soil map using the instructions from the input -seed_to_soil_map = defaultdict(lambda: seed_to_soil_map) -soil_to_fertilizer_map = defaultdict(lambda: soil_to_fertilizer_map) -fertilizer_to_water_map = defaultdict(lambda: fertilizer_to_water_map) -water_to_light_map = defaultdict(lambda: water_to_light_map) -light_to_temperature_map = defaultdict(lambda: light_to_temperature_map) -temperature_to_humidity_map = defaultdict(lambda: temperature_to_humidity_map) -humidity_to_location_map = defaultdict(lambda: humidity_to_location_map) +# seed_to_soil_map = defaultdict(lambda: seed_to_soil_map) +# soil_to_fertilizer_map = defaultdict(lambda: soil_to_fertilizer_map) +# fertilizer_to_water_map = defaultdict(lambda: fertilizer_to_water_map) +# water_to_light_map = defaultdict(lambda: water_to_light_map) +# light_to_temperature_map = defaultdict(lambda: light_to_temperature_map) +# temperature_to_humidity_map = defaultdict(lambda: temperature_to_humidity_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(soil_to_fertilizer_map_instructions, soil_to_fertilizer_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(light_to_temperature_map_instructions, light_to_temperature_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(seed_to_soil_map_instructions, seed_to_soil_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(water_to_light_map_instructions, water_to_light_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(humidity_to_location_map_instructions, humidity_to_location_map) locations = [] -# now we run the seed number through the maps 1 by 1 to get the final location for seed_number in seed_numbers: - soil = seed_to_soil_map[int(seed_number)] - fertilizer = soil_to_fertilizer_map[soil] - water = fertilizer_to_water_map[fertilizer] - light = water_to_light_map[water] - temperature = light_to_temperature_map[light] - humidity = temperature_to_humidity_map[temperature] - location = humidity_to_location_map[humidity] + soil = map_value(int(seed_number), seed_to_soil_map_instructions) + fertilizer = map_value(soil, soil_to_fertilizer_map_instructions) + water = map_value(fertilizer, fertilizer_to_water_map_instructions) + light = map_value(water, water_to_light_map_instructions) + temperature = map_value(light, light_to_temperature_map_instructions) + humidity = map_value(temperature, temperature_to_humidity_map_instructions) + location = map_value(humidity, humidity_to_location_map_instructions) locations.append(location) #print lowest location