diff --git a/README.md b/README.md index cbb9d87..bc6bc96 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # Advent of Code 2ß23 + \ No newline at end of file diff --git a/day05.py b/day05.py index 354daf4..2b02335 100644 --- a/day05.py +++ b/day05.py @@ -2,13 +2,6 @@ from tools.aoc import AOCDay from typing import Any -def lookup(the_map: dict, source: int) -> int: - for (s, l), d in the_map.items(): - if s <= source < s + l: - return source - s + d - return source - - class Day(AOCDay): inputs = [ [ @@ -21,29 +14,15 @@ class Day(AOCDay): ], ] - def __init__(self, year: int, day: int): - super().__init__(year, day) - self.seed_locations = {} - - def get_seed_location(self, seed: int) -> int: - soil = lookup(self.seed2soil, seed) - fertilizer = lookup(self.soil2fertilizer, soil) - water = lookup(self.fertilizer2water, fertilizer) - light = lookup(self.water2light, water) - temperature = lookup(self.light2temperature, light) - humidity = lookup(self.temperature2humidity, temperature) - location = lookup(self.humidity2location, humidity) - return location - - def parse_input(self, part2: bool = False) -> list: + def parse_input(self) -> list: seeds = [] - self.seed2soil = {} - self.soil2fertilizer = {} - self.fertilizer2water = {} - self.water2light = {} - self.light2temperature = {} - self.temperature2humidity = {} - self.humidity2location = {} + seed2soil = {} + soil2fertilizer = {} + fertilizer2water = {} + water2light = {} + light2temperature = {} + temperature2humidity = {} + humidity2location = {} the_map = None for line in self.getInput(): if not line: @@ -52,36 +31,49 @@ class Day(AOCDay): _, s = line.split(": ") seeds = list(map(int, s.split())) elif line.startswith("seed-to-soil"): - the_map = self.seed2soil + the_map = seed2soil elif line.startswith("soil-to-fertilizer"): - the_map = self.soil2fertilizer + the_map = soil2fertilizer elif line.startswith("fertilizer-to-water"): - the_map = self.fertilizer2water + the_map = fertilizer2water elif line.startswith("water-to-light"): - the_map = self.water2light + the_map = water2light elif line.startswith("light-to-temperature"): - the_map = self.light2temperature + the_map = light2temperature elif line.startswith("temperature-to-humidity"): - the_map = self.temperature2humidity + the_map = temperature2humidity elif line.startswith("humidity-to-location"): - the_map = self.humidity2location + the_map = humidity2location else: dest, source, length = map(int, line.split()) - the_map[(source, length)] = dest - - self.seed_locations = {} - last_min = 0 - for (source, length), dest in sorted(self.seed2soil.items()): - if last_min < source: - self.seed2soil[(last_min, source - last_min)] = 0 - last_min = source + length - self.seed2soil[(last_min, 2**32)] = 0 + 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() - return min(self.get_seed_location(s) for s in seeds) def part2(self) -> Any: return