aoc2023/day05.py
2024-11-30 11:35:11 +01:00

95 lines
3.0 KiB
Python

from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(35, "input5_test"),
(346433842, "input5"),
],
[
(46, "input5_test"),
(60294664, "input5"),
],
]
def parse_input(self) -> list:
seeds = []
seed2soil = {}
soil2fertilizer = {}
fertilizer2water = {}
water2light = {}
light2temperature = {}
temperature2humidity = {}
humidity2location = {}
the_map = None
for line in self.getInput():
if not line:
continue
elif line.startswith("seeds:"):
_, s = line.split(": ")
seeds = list(map(int, s.split()))
elif line.startswith("seed-to-soil"):
the_map = seed2soil
elif line.startswith("soil-to-fertilizer"):
the_map = soil2fertilizer
elif line.startswith("fertilizer-to-water"):
the_map = fertilizer2water
elif line.startswith("water-to-light"):
the_map = water2light
elif line.startswith("light-to-temperature"):
the_map = light2temperature
elif line.startswith("temperature-to-humidity"):
the_map = temperature2humidity
elif line.startswith("humidity-to-location"):
the_map = humidity2location
else:
dest, source, length = map(int, line.split())
the_map[(dest, length)] = source
loc2seed = self.augment_map(humidity2location, temperature2humidity)
print(loc2seed)
print(self.lookup(loc2seed, 82))
loc2seed = self.augment_map(loc2seed, light2temperature)
print(self.lookup(loc2seed, 82))
return seeds
def augment_map(self, source_map: dict, target_map: dict) -> dict:
last_min = 0
the_map = {}
for (dest, length), source in sorted(source_map.items()):
if last_min < dest:
the_map[(last_min, dest - last_min)] = self.lookup(target_map, last_min)
the_map[(dest, length)] = self.lookup(target_map, source)
last_min = dest + length
return the_map
def lookup(self, the_map: dict, target: int) -> int:
for (dest, length), source in the_map.items():
if dest <= target < dest + length:
return source + target - dest
return target
def part1(self) -> Any:
seeds = self.parse_input()
def part2(self) -> Any:
return
seeds = self.parse_input()
min_loc = 2**32
for i in range(0, len(seeds), 2):
for s in range(seeds[i], seeds[i] + seeds[i + 1]):
x = self.get_seed_location(s)
if x < min_loc:
min_loc = x
print(i, "=>", min_loc)
return min_loc
if __name__ == "__main__":
day = Day(2023, 5)
day.run(verbose=True)