diff --git a/advent/days/day11/solution.py b/advent/days/day11/solution.py index 5c43bf3..4bb2379 100644 --- a/advent/days/day11/solution.py +++ b/advent/days/day11/solution.py @@ -53,7 +53,7 @@ class Parser: worry_inc, test, lambda number, items, worry_inc, test: Monkey(number, items, worry_inc, *test)) - monkey_list: P[list[Monkey]] = P.first(monkey, P.eol().optional()).many() + monkey_list: P[list[Monkey]] = P.second(P.eol().optional(), monkey).many() WorryIncreaser = Callable[[int], int] @@ -111,7 +111,7 @@ class Troop_While_Worried(Troop): """ The """ @classmethod def parse(cls, lines: Iterator[str]) -> Self: - monkeys = Parser.monkey_list.parse_iterator(lines).get() + monkeys = Parser.monkey_list.parse(lines).get() return Troop_While_Worried(monkeys) def single_round(self): @@ -126,7 +126,7 @@ class Troop_While_Kinda_Relieved(Troop): @classmethod def parse(cls, lines: Iterator[str]) -> Self: - monkeys = Parser.monkey_list.parse_iterator(lines).get() + monkeys = Parser.monkey_list.parse(lines).get() return Troop_While_Kinda_Relieved(monkeys, prod(monkey.modulator for monkey in monkeys)) def single_round(self): diff --git a/advent/parser/parser.py b/advent/parser/parser.py index 79163ec..d55fdfb 100644 --- a/advent/parser/parser.py +++ b/advent/parser/parser.py @@ -112,24 +112,27 @@ class P(Generic[T]): def __init__(self, func: ParserFunc[T]): self.func = func - def parse(self, s: str) -> Result[T]: - all_results = self.func(SimpleParserInput(s, 0)) + def parse(self, input: str | Iterator[str]) -> Result[T]: + if isinstance(input, str): + parser_input = SimpleParserInput(input, 0) + else: + parser_input = IteratorParserInput(StringDispenser(input), 0) + + all_results = self.func(parser_input) try: _, result = next(all_results) return Result.of(result) except StopIteration: return Result.fail("No result") - def parse_iterator(self, it: Iterator[str]) -> Result[T]: - all_results = self.func(IteratorParserInput(StringDispenser(it), 0)) - try: - _, result = next(all_results) - return Result.of(result) - except StopIteration: - return Result.fail("No result") + def parse_multi(self, input: str | Iterator[str]) -> Iterator[T]: + if isinstance(input, str): + parser_input = SimpleParserInput(input, 0) + else: + parser_input = IteratorParserInput(StringDispenser(input), 0) - def parse_multi(self, s: str, i: int = 0) -> Iterator[T]: - return (v for _, v in self.func(SimpleParserInput(s, i))) + all_results = self.func(parser_input) + return (v for _, v in all_results) @classmethod def pure(cls, value: T) -> P[T]: diff --git a/advent/parser/test_parser.py b/advent/parser/test_parser.py index 6c5d9ed..2593743 100644 --- a/advent/parser/test_parser.py +++ b/advent/parser/test_parser.py @@ -275,7 +275,7 @@ def test_iterator_input(): input = iter(['1', '2']) parser = P.unsigned().line().many() expected = [1, 2] - result = parser.parse_iterator(input).get() + result = parser.parse(input).get() assert result == expected @@ -283,5 +283,5 @@ def test_iterator_trim_input(): input = iter(['1 ', '2 ']) parser = P.unsigned().trim().line().many() expected = [1, 2] - result = parser.parse_iterator(input).get() + result = parser.parse(input).get() assert result == expected