generated from public/aoc_template
tiles?
This commit is contained in:
parent
e2acccf2e5
commit
b713a4ddbf
@ -1,3 +1,4 @@
|
|||||||
# Advent of Code 2ß23
|
# Advent of Code 2ß23
|
||||||
<!-- AOC TILES BEGIN -->
|
<!-- AOC TILES BEGIN -->
|
||||||
|
|
||||||
<!-- AOC TILES END -->
|
<!-- AOC TILES END -->
|
||||||
84
day05.py
84
day05.py
@ -2,13 +2,6 @@ from tools.aoc import AOCDay
|
|||||||
from typing import Any
|
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):
|
class Day(AOCDay):
|
||||||
inputs = [
|
inputs = [
|
||||||
[
|
[
|
||||||
@ -21,29 +14,15 @@ class Day(AOCDay):
|
|||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, year: int, day: int):
|
def parse_input(self) -> list:
|
||||||
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:
|
|
||||||
seeds = []
|
seeds = []
|
||||||
self.seed2soil = {}
|
seed2soil = {}
|
||||||
self.soil2fertilizer = {}
|
soil2fertilizer = {}
|
||||||
self.fertilizer2water = {}
|
fertilizer2water = {}
|
||||||
self.water2light = {}
|
water2light = {}
|
||||||
self.light2temperature = {}
|
light2temperature = {}
|
||||||
self.temperature2humidity = {}
|
temperature2humidity = {}
|
||||||
self.humidity2location = {}
|
humidity2location = {}
|
||||||
the_map = None
|
the_map = None
|
||||||
for line in self.getInput():
|
for line in self.getInput():
|
||||||
if not line:
|
if not line:
|
||||||
@ -52,36 +31,49 @@ class Day(AOCDay):
|
|||||||
_, s = line.split(": ")
|
_, s = line.split(": ")
|
||||||
seeds = list(map(int, s.split()))
|
seeds = list(map(int, s.split()))
|
||||||
elif line.startswith("seed-to-soil"):
|
elif line.startswith("seed-to-soil"):
|
||||||
the_map = self.seed2soil
|
the_map = seed2soil
|
||||||
elif line.startswith("soil-to-fertilizer"):
|
elif line.startswith("soil-to-fertilizer"):
|
||||||
the_map = self.soil2fertilizer
|
the_map = soil2fertilizer
|
||||||
elif line.startswith("fertilizer-to-water"):
|
elif line.startswith("fertilizer-to-water"):
|
||||||
the_map = self.fertilizer2water
|
the_map = fertilizer2water
|
||||||
elif line.startswith("water-to-light"):
|
elif line.startswith("water-to-light"):
|
||||||
the_map = self.water2light
|
the_map = water2light
|
||||||
elif line.startswith("light-to-temperature"):
|
elif line.startswith("light-to-temperature"):
|
||||||
the_map = self.light2temperature
|
the_map = light2temperature
|
||||||
elif line.startswith("temperature-to-humidity"):
|
elif line.startswith("temperature-to-humidity"):
|
||||||
the_map = self.temperature2humidity
|
the_map = temperature2humidity
|
||||||
elif line.startswith("humidity-to-location"):
|
elif line.startswith("humidity-to-location"):
|
||||||
the_map = self.humidity2location
|
the_map = humidity2location
|
||||||
else:
|
else:
|
||||||
dest, source, length = map(int, line.split())
|
dest, source, length = map(int, line.split())
|
||||||
the_map[(source, length)] = dest
|
the_map[(dest, length)] = source
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
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
|
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:
|
def part1(self) -> Any:
|
||||||
seeds = self.parse_input()
|
seeds = self.parse_input()
|
||||||
return min(self.get_seed_location(s) for s in seeds)
|
|
||||||
|
|
||||||
def part2(self) -> Any:
|
def part2(self) -> Any:
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user