aoc/23/day5/main.py
2023-12-05 15:24:19 +01:00

85 lines
2.4 KiB
Python
Executable file

import math
seeds = []
mappings = {}
with open("input.txt", "r") as f:
current_map = None
for line in f:
line = line.strip()
if line == "":
continue
elif line.startswith("seeds:"):
seeds = list(map(int, line.removeprefix("seeds: ").split(" ")))
elif line.endswith(" map:"):
x, _, y = line.removesuffix(" map:").split("-")
current_map = (x, y)
mappings[x] = {"next": y, "maps": []}
else:
dest_start, src_start, length = list(map(int, line.split(" ")))
mappings[current_map[0]]["maps"].append((dest_start, src_start, length))
locations = []
for seed in seeds:
current_type = "seed"
current_number = seed
while current_type != "location":
mapping = mappings[current_type]
found_dest = None
for dest_start, src_start, length in mapping["maps"]:
if current_number in range(src_start, src_start + length):
found_dest = dest_start + (current_number - src_start)
break
if found_dest == None:
found_dest = current_number
current_type = mapping["next"]
current_number = found_dest
locations.append(current_number)
print("part1", min(locations))
def partition(lst, size):
for i in range(0, len(lst) // size):
yield lst[i::size]
# current_type = "seed"
# ranges = []
lowest = math.inf
first_seed = None
for seed in seeds:
if first_seed == None:
first_seed = seed
continue
second_seed = seed
print("Lezgo", first_seed, second_seed)
# ranges.append((first_seed, first_seed + second_seed))
for seed in range(first_seed, first_seed + second_seed):
# print((seed - first_seed) / second_seed * 100)
current_type = "seed"
current_number = seed
while current_type != "location":
mapping = mappings[current_type]
found_dest = None
for dest_start, src_start, length in mapping["maps"]:
if current_number in range(src_start, src_start + length):
found_dest = dest_start + (current_number - src_start)
break
if found_dest == None:
found_dest = current_number
current_type = mapping["next"]
current_number = found_dest
if current_number < lowest:
lowest = current_number
first_seed = None
print("part2", lowest)