Improve input parsing

This commit is contained in:
2024-12-08 08:26:25 -08:00
parent b9f544ed1e
commit 569714d227
8 changed files with 19 additions and 18 deletions

View File

@@ -7,7 +7,7 @@ test_input = """
1 3 1 3
3 9 3 9
3 3 3 3
""".strip() """
test_solution_p1 = 11 test_solution_p1 = 11
test_solution_p2 = 31 test_solution_p2 = 31
@@ -28,6 +28,6 @@ def solve_p2(puzzle_input: str) -> int:
def _parse_lists(puzzle_input: str) -> tuple[list[int], list[int]]: def _parse_lists(puzzle_input: str) -> tuple[list[int], list[int]]:
lines = (line.partition(" ") for line in puzzle_input.split("\n") if line) lines = (line.partition(" ") for line in puzzle_input.strip().split("\n"))
list1, list2 = zip(*((int(line[0]), int(line[2])) for line in lines)) list1, list2 = zip(*((int(line[0]), int(line[2])) for line in lines))
return list1, list2 return list1, list2

View File

@@ -10,7 +10,7 @@ test_input = """
1 3 2 4 5 1 3 2 4 5
8 6 4 4 1 8 6 4 4 1
1 3 6 7 9 1 3 6 7 9
""".strip() """
test_solution_p1 = 2 test_solution_p1 = 2
test_solution_p2 = 4 test_solution_p2 = 4
@@ -41,7 +41,7 @@ def solve_p2(puzzle_input: str) -> int:
def _parse_reports(puzzle_input: str) -> Iterator[tuple[int, ...]]: def _parse_reports(puzzle_input: str) -> Iterator[tuple[int, ...]]:
lines = puzzle_input.splitlines() lines = puzzle_input.strip().splitlines()
return (tuple(int(level) for level in line.split(" ")) for line in lines) return (tuple(int(level) for level in line.split(" ")) for line in lines)

View File

@@ -1,12 +1,13 @@
import re import re
from functools import reduce from functools import reduce
test_input_p1 = ( test_input_p1 = """
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))" xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
) """
test_input_p2 = (
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" test_input_p2 = """
) xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
"""
test_solution_p1 = 161 test_solution_p1 = 161
test_solution_p2 = 48 test_solution_p2 = 48

View File

@@ -11,7 +11,7 @@ SMSMSASXSS
SAXAMASAAA SAXAMASAAA
MAMMMXMMMM MAMMMXMMMM
MXMXAXMASX MXMXAXMASX
""".strip() """
test_solution_p1 = 18 test_solution_p1 = 18
test_solution_p2 = 9 test_solution_p2 = 9
@@ -29,7 +29,7 @@ def solve_p2(puzzle_input: str) -> int:
def _parse_world_grid(puzzle_input: str) -> tuple[tuple[str, ...], ...]: def _parse_world_grid(puzzle_input: str) -> tuple[tuple[str, ...], ...]:
return tuple(tuple(row) for row in puzzle_input.splitlines()) return tuple(tuple(row) for row in puzzle_input.strip().splitlines())
def _scan_word_grid( def _scan_word_grid(

View File

@@ -29,7 +29,7 @@ test_input = """
75,97,47,61,53 75,97,47,61,53
61,13,29 61,13,29
97,13,75,29,47 97,13,75,29,47
""".strip() """
test_solution_p1 = 143 test_solution_p1 = 143
test_solution_p2 = 123 test_solution_p2 = 123

View File

@@ -11,7 +11,7 @@ test_input = """
........#. ........#.
#......... #.........
......#... ......#...
""".strip() """
test_solution_p1 = 41 test_solution_p1 = 41
test_solution_p2 = 6 test_solution_p2 = 6
@@ -51,7 +51,7 @@ def _parse_map(puzzle_input: str) -> MapInfo:
puzzle_map = [] puzzle_map = []
guard = None guard = None
for x, line in enumerate(puzzle_input.split("\n")): for x, line in enumerate(puzzle_input.strip().split("\n")):
if not line: if not line:
continue continue
line_list = list() line_list = list()

View File

@@ -13,7 +13,7 @@ test_input = """
192: 17 8 14 192: 17 8 14
21037: 9 7 18 13 21037: 9 7 18 13
292: 11 6 16 20 292: 11 6 16 20
""".strip() """
test_solution_p1 = 3749 test_solution_p1 = 3749
test_solution_p2 = 11387 test_solution_p2 = 11387
@@ -50,7 +50,7 @@ class Equation(NamedTuple):
def _parse_equations(puzzle_input: str) -> Iterator[Equation]: def _parse_equations(puzzle_input: str) -> Iterator[Equation]:
for line in puzzle_input.split("\n"): for line in puzzle_input.strip().split("\n"):
if line: if line:
result_string, _, factors_string = line.partition(": ") result_string, _, factors_string = line.partition(": ")
yield Equation( yield Equation(

View File

@@ -14,7 +14,7 @@ test_input = """
.........A.. .........A..
............ ............
............ ............
""".strip() """
test_solution_p1 = 14 test_solution_p1 = 14
test_solution_p2 = 34 test_solution_p2 = 34