Day 05, something weird
This commit is contained in:
parent
182f1c2dbc
commit
fda10274ee
1 changed files with 63 additions and 26 deletions
89
day05.py
89
day05.py
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
import bisect
|
||||||
|
|
||||||
|
|
||||||
import regex as re
|
import regex as re
|
||||||
|
@ -8,37 +9,65 @@ import regex as re
|
||||||
from aoc import Aoc2, AocSameParser
|
from aoc import Aoc2, AocSameParser
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Map:
|
class Map:
|
||||||
source: str
|
def __init__(self, group: list[str]) -> None:
|
||||||
dest: str
|
self.maps = []
|
||||||
start_source: int
|
|
||||||
start_dest: int
|
|
||||||
id_range: int
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def from_group(group: list[str]) -> list[Map]:
|
|
||||||
header_match = re.match(r"(?P<source>[^-]*)-to-(?P<dest>[^ ]*) map:", group[0])
|
|
||||||
if header_match is None:
|
|
||||||
raise RuntimeError(f'Could not parse "{group[0]}" as group header')
|
|
||||||
|
|
||||||
source = header_match["source"]
|
|
||||||
dest = header_match["dest"]
|
|
||||||
|
|
||||||
maps = []
|
|
||||||
for line in group[1:]:
|
for line in group[1:]:
|
||||||
start_source, start_dest, id_range = [
|
start_dest, start_source, id_range = [
|
||||||
int(d) for d in re.findall(r"\d+", line)
|
int(d) for d in re.findall(r"\d+", line)
|
||||||
]
|
]
|
||||||
maps.append(Map(source, dest, start_source, start_dest, id_range))
|
bisect.insort(self.maps, (start_source, start_dest, id_range))
|
||||||
return maps
|
|
||||||
|
def __getitem__(self, x: int) -> int:
|
||||||
|
print(f"In {x}")
|
||||||
|
for (source, dest, rnge) in self.maps:
|
||||||
|
print(f"Source {source}")
|
||||||
|
if x >= source and x < source + rnge:
|
||||||
|
return dest + (x - source)
|
||||||
|
return x
|
||||||
|
# index = bisect.bisect_left(self.maps, (x+1,0,0))
|
||||||
|
# index -= 1
|
||||||
|
# if index < 0:
|
||||||
|
# return x
|
||||||
|
#
|
||||||
|
# source, dest, id_range = self.maps[index]
|
||||||
|
# if x < source + id_range:
|
||||||
|
# return dest + (x - source)
|
||||||
|
# return x
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
l = []
|
||||||
|
for k, v, x in self.maps:
|
||||||
|
l.append(f"{k} - {k + x}: {v} - {v + x}")
|
||||||
|
return f"Map({', '.join(l)})"
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Problem:
|
class Problem:
|
||||||
seeds: list[int]
|
seeds: list[int]
|
||||||
maps: list[Map]
|
maps: dict[tuple[str,str],Map]
|
||||||
|
|
||||||
|
def convert(self, cat_from: str, cat_to:str, value: int) -> int:
|
||||||
|
target = cat_from
|
||||||
|
output_value = value
|
||||||
|
while target != cat_to:
|
||||||
|
new_target= list(filter(lambda fromto: fromto[0] == target, self.maps.keys()))[0][1]
|
||||||
|
print(f"{target}({output_value})")
|
||||||
|
output_value = self.maps[(target, new_target)][output_value]
|
||||||
|
print(f"{new_target}({output_value})")
|
||||||
|
target = new_target
|
||||||
|
return output_value
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse_header(line: str) -> tuple[str, str]:
|
||||||
|
header_match = re.match(r"(?P<source>[^-]*)-to-(?P<dest>[^ ]*) map:", line)
|
||||||
|
if header_match is None:
|
||||||
|
raise RuntimeError(f'Could not parse "{line}" as group header')
|
||||||
|
|
||||||
|
return header_match["source"], header_match["dest"]
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_input(inpt: str) -> Problem:
|
def from_input(inpt: str) -> Problem:
|
||||||
lines = inpt.splitlines()
|
lines = inpt.splitlines()
|
||||||
|
@ -53,7 +82,7 @@ class Problem:
|
||||||
groups.append([])
|
groups.append([])
|
||||||
else:
|
else:
|
||||||
groups[-1].append(line)
|
groups[-1].append(line)
|
||||||
maps = list(chain.from_iterable(Map.from_group(group) for group in groups))
|
maps = {Problem.parse_header(group[0]): Map(group[1:]) for group in groups}
|
||||||
return Problem(seeds, maps)
|
return Problem(seeds, maps)
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,16 +93,24 @@ class Day05(AocSameParser[Problem], Aoc2[Problem, Problem]):
|
||||||
def parseinput(self, inpt: str) -> Problem:
|
def parseinput(self, inpt: str) -> Problem:
|
||||||
return Problem.from_input(inpt)
|
return Problem.from_input(inpt)
|
||||||
|
|
||||||
def part1(self, inpt: Problem) -> None:
|
def part1(self, inpt: Problem) -> int:
|
||||||
print(inpt)
|
return min(inpt.convert("seed", "location", x) for x in inpt.seeds)
|
||||||
|
|
||||||
|
|
||||||
|
def part2(self, inpt: Problem) -> int:
|
||||||
|
print(inpt.maps[("light", "temperature")])
|
||||||
|
print(inpt.convert("seed", "location", 82))
|
||||||
|
# seed_iter = iter(inpt.seeds)
|
||||||
|
# seeds = []
|
||||||
|
# for start, rnge in zip(seed_iter, seed_iter):
|
||||||
|
# seeds.extend(range(start,start+rnge))
|
||||||
|
# print([inpt.convert("seed", "location", x) for x in seeds])
|
||||||
|
|
||||||
def part2(self, inpt: Problem) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
day5 = Day05()
|
day5 = Day05()
|
||||||
day5.run_example_1()
|
day5.run_example_2()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Add table
Reference in a new issue