from helpers import Helper helper = Helper(debug=True) load_input = helper.load_input debug = helper.debug def main(): lines = load_input(5) maps_dict = { "seeds": [], "seed-to-soil": [], "soil-to-fertilizer": [], "fertilizer-to-water": [], "water-to-light": [], "light-to-temperature": [], "temperature-to-humidity": [], "humidity-to-location": [], } maps_keys = list(maps_dict.keys()) maps_dict["seeds"] = [int(n) for n in lines[0].split()[1:]] seeds_list = [{"seed": seed} for seed in maps_dict["seeds"]] current_key = "" for line in lines[1:]: if line == "": continue split_line = line.split() if split_line[0] in maps_keys: current_key = split_line[0] else: maps_dict[current_key].append({ x: int(a) for (x, a) in zip(["destination", "source", "length"], split_line)} ) for seed in seeds_list: for key in maps_keys[1:]: source, _, destination = key.split("-") i = 0 t_list = maps_dict[key] while i < len(t_list): s_map = t_list[i] if seed[source] in range(s_map["source"], (s_map["source"] + s_map["length"])): source_distance = seed[source] - s_map["source"] seed[destination] = s_map["destination"] + source_distance debug("{}: {} found in {}+{}, {} is {} + {}".format( source, seed[source], s_map["source"], s_map["length"], seed[destination], s_map["destination"], source_distance )) i = len(t_list) else: debug("{}: {} not found in {}+{}, using original".format( source, seed[source], s_map["source"], s_map["length"] )) i += 1 if not seed.get(destination, None): seed[destination] = seed[source] print(seeds_list) print(min(seeds_list, key=lambda x: x["location"])) if __name__ == "__main__": main()