#!/usr/bin/env python3.11 # Is killed on WSL2 after vmmem increase to 16GB import re seeds = [] maps = { "seed-to-soil": {}, "soil-to-fertilizer": {}, "fertilizer-to-water": {}, "water-to-light": {}, "light-to-temperature": {}, "temperature-to-humidity": {}, "humidity-to-location": {}, } input_file = "example_input.txt" with open(input_file) as input: current_map = {} while line := input.readline(): if match := re.match("seeds: (.*)", line): for seed in match.group(1).split(" "): seeds.append(int(seed)) continue if match := re.match("([a-z-]+) map:", line): current_map = maps[match.group(1)] continue if match := re.match("([\d ]+)", line): destination, source, length = match.group(1).split(" ") for idx in range(int(length)): current_map[int(source) + idx] = int(destination) + idx lowest_location = None for seed in seeds: soil = fertilizer = water = light = temperature = humidity = location = -1 soil = maps["seed-to-soil"][seed] if seed in maps["seed-to-soil"] else seed fertilizer = maps["soil-to-fertilizer"][soil] if soil in maps["soil-to-fertilizer"] else soil water = maps["fertilizer-to-water"][fertilizer] if fertilizer in maps["fertilizer-to-water"] else fertilizer light = maps["water-to-light"][water] if water in maps["water-to-light"] else water temperature = maps["light-to-temperature"][light] if light in maps["light-to-temperature"] else light humidity = ( maps["temperature-to-humidity"][temperature] if temperature in maps["temperature-to-humidity"] else temperature ) location = maps["humidity-to-location"][humidity] if humidity in maps["humidity-to-location"] else humidity if lowest_location is not None: lowest_location = min(lowest_location, location) else: lowest_location = location print(lowest_location)