From f234c0964bc88aed6887967473bcd9b6b10701da Mon Sep 17 00:00:00 2001 From: Dan Katzuv Date: Sat, 10 Dec 2022 22:12:06 +0200 Subject: [PATCH] Add function that parses section IDs from the input --- puzzles/solutions/2022/d04/p1.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/puzzles/solutions/2022/d04/p1.py b/puzzles/solutions/2022/d04/p1.py index c3bbf496..69e9d35d 100644 --- a/puzzles/solutions/2022/d04/p1.py +++ b/puzzles/solutions/2022/d04/p1.py @@ -1,6 +1,23 @@ import sys +def get_sections(input_text: str) -> tuple[tuple[set[int], set[int]]]: + """ + :param input_text: puzzle input + :return: tuple of (first section IDs, second section IDs) pairs + """ + pairs = input_text.splitlines() + sections = [] + for pair in pairs: + pair_sections = [] + for section_range in pair.split(","): + start, end = map(int, section_range.split("-")) + section_ids = set(range(start, end + 1)) + pair_sections.append(section_ids) + sections.append(tuple(pair_sections)) + return tuple(sections) + + def get_answer(input_text: str): raise NotImplementedError