Skip to content

Commit

Permalink
Python: use the generic parser for more days
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacG committed Dec 3, 2024
1 parent a7a25b7 commit 083cd9e
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 13 deletions.
1 change: 0 additions & 1 deletion 2017/d20.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Day20(aoc.Challenge):
TESTS = [
aoc.TestCase(part=1, inputs=SAMPLE[0], want=0),
aoc.TestCase(part=2, inputs=SAMPLE[1], want=1),
# aoc.TestCase(part=2, inputs=SAMPLE[0], want=aoc.TEST_SKIP),
]
INPUT_PARSER = aoc.parse_ints_per_line

Expand Down
5 changes: 2 additions & 3 deletions 2018/d08.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ class Day08(aoc.Challenge):
aoc.TestCase(inputs=SAMPLE, part=1, want=138),
aoc.TestCase(inputs=SAMPLE, part=2, want=66),
]
INPUT_PARSER = aoc.parse_multi_int_per_line

def part1(self, puzzle_input: InputType) -> int:
"""Return the sum of all nodes."""
reader = iter(puzzle_input[0])
reader = iter(puzzle_input)

def parser():
child_count = next(reader)
Expand All @@ -31,7 +30,7 @@ def parser():

def part2(self, puzzle_input: InputType) -> int:
"""Return the value of the root node."""
reader = iter(puzzle_input[0])
reader = iter(puzzle_input)

def parser():
child_count = next(reader)
Expand Down
1 change: 0 additions & 1 deletion 2021/d24.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ class Day24(aoc.Challenge):
for k, v in input_data.DAY24.items()
for p in (0, 1)
]
INPUT_PARSER = aoc.parse_multi_str_per_line

def part1(self, puzzle_input: list[tuple[str, ...]]) -> int:
"""Return the max valid seriel number."""
Expand Down
1 change: 0 additions & 1 deletion 2023/d07.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Day07(aoc.Challenge):
aoc.TestCase(inputs=SAMPLE, part=1, want=6440),
aoc.TestCase(inputs=SAMPLE, part=2, want=5905),
]
INPUT_PARSER = aoc.parse_multi_str_per_line

def sort(self, part_two: int, line: list[str]) -> tuple[int, int]:
"""Sort cards based on ranking then highest card."""
Expand Down
1 change: 0 additions & 1 deletion 2023/d09.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Day09(aoc.Challenge):
aoc.TestCase(inputs=SAMPLE, part=1, want=114),
aoc.TestCase(inputs=SAMPLE, part=2, want=2),
]
INPUT_PARSER = aoc.parse_multi_int_per_line

def get_prior_and_following(self, line) -> tuple[int, int]:
"""Return the prior and following value of a line using recursive first differences."""
Expand Down
1 change: 0 additions & 1 deletion 2023/d18.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Day18(aoc.Challenge):
aoc.TestCase(inputs=SAMPLE, part=1, want=62),
aoc.TestCase(inputs=SAMPLE, part=2, want=952408144115),
]
INPUT_PARSER = aoc.parse_multi_str_per_line

def solver(self, puzzle_input: list[list[str]], part_one: bool) -> int:
# Start at 0, 0 and track vertical trenches.
Expand Down
1 change: 0 additions & 1 deletion 2023/d24.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class Day24(aoc.Challenge):
aoc.TestCase(inputs=SAMPLE, part=1, want=2),
aoc.TestCase(inputs=SAMPLE, part=2, want=aoc.TEST_SKIP),
]
INPUT_PARSER = aoc.parse_re_findall_int(aoc.RE_INT)
TIMEOUT = 60

def part1(self, puzzle_input: InputType) -> int:
Expand Down
12 changes: 9 additions & 3 deletions pylib/aoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}

RE_INT = re.compile(r"[+-]?\d+")
RE_BOUNDED_INT = re.compile(r"\b[+-]?\d+\b")
RE_BOUNDED_INT = re.compile(r"[+-]?\b\d+\b")
OPERATORS = {
">": operator.gt,
">=": operator.ge,
Expand Down Expand Up @@ -797,8 +797,14 @@ def _parser(self) -> parsers.BaseParser:
lines = data.splitlines()
multi_lines = len(lines) > 1
one_line = lines[0]
if RE_INT.fullmatch(one_line):

# Only numbers on each line.
if all(RE_INT.fullmatch(line) for line in lines):
return parse_one_int_per_line if multi_lines else parse_one_int
number_line = re.compile(r"^[+-]?\d+( +[+-]?\d+)+$")
if all(number_line.fullmatch(line) for line in lines):
return parse_ints_per_line if multi_lines else parse_ints_one_line

if len(RE_BOUNDED_INT.findall(one_line)) > 1 and multi_lines:
lines = [re.sub(" +", " ", line) for line in lines[:4]]
one_line = lines[0]
Expand All @@ -810,7 +816,7 @@ def _parser(self) -> parsers.BaseParser:
word_count = max(len(line.split()) for line in data.splitlines())
if word_count == 1:
return parse_one_str_per_line if multi_lines else parse_one_str
return parse_multiple_words_per_line if multi_lines else parse_one_str
return parse_multi_str_per_line if multi_lines else parse_one_str

def input_parser(self, puzzle_input: str) -> Any:
"""Parse input data. Block of text -> output."""
Expand Down
1 change: 0 additions & 1 deletion pylib/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ def parse(self, puzzle_input: str) -> tuple[tuple[int, int], list[set[complex]]]
parse_re_findall_str = lambda x: BaseParseReFindall(x, input_to_strs)

# Convert the input into list[list[int]], splitting each line into multiple words.
parse_multi_int_per_line = BaseParseMultiPerLine(input_to_ints)
parse_re_group_int = lambda x: BaseParseReGroups(x, input_to_ints)
parse_re_findall_int = lambda x: BaseParseReFindall(x, input_to_ints)
parse_ints = BaseParseReFindall(RE_INT, input_to_ints)
Expand Down

0 comments on commit 083cd9e

Please sign in to comment.