from helpers import Helper helper = Helper(debug=True) load_input = helper.load_input debug = helper.debug def within(target, start, end): if start == end: return target == start if start > end: start, end = end, start return target >= start and end >= target 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:]] real_seeds = [] i, j = 0, 1 while j < len(maps_dict["seeds"]): real_seeds.append((maps_dict["seeds"][i], maps_dict["seeds"][i] + maps_dict["seeds"][j])) # new_start = maps_dict["seeds"][i] # new_end = new_start + maps_dict["seeds"][j] # debug(f"Adding seeds in range {new_start}-{new_end}") # new_seeds = range(new_start, new_end) # debug(new_seeds) # real_seeds.append(new_seeds) # debug(real_seeds) i += 2 j += 2 seeds_list = [{"seed": seed[1]} for seed in real_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] # debug(f"Checking {seed[source]} against {s_map['source']}, {s_map['source'] + s_map['length']}") if within(seed[source], 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] debug("\n".join([f"{k}: {v}" for k, v in seed.items() for seed in seeds_list])) print(min(seeds_list, key=lambda x: x["location"])) if __name__ == "__main__": main()