From 4acef023c25579f216816043d9c3940851e32462 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 3 Dec 2022 22:15:10 +0100 Subject: [PATCH] Solve day3 part 2 --- 2022/day03_rucksack.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/2022/day03_rucksack.py b/2022/day03_rucksack.py index e7187a3..5821e53 100644 --- a/2022/day03_rucksack.py +++ b/2022/day03_rucksack.py @@ -7,8 +7,8 @@ def main(filename: str, expected_part_1: int = None, expected_part_2: int = None with open(filename) as f: data = f.read().strip().split("\n") - data = parse_data(data) - solution_part_1 = solve_part_1(data) + data_1 = parse_data(data) + solution_part_1 = solve_part_1(data_1) print(f"1. Found {solution_part_1}") if expected_part_1: @@ -41,10 +41,19 @@ def solve_part_1(data: DataType) -> int: return total -def solve_part_2(data: DataType) -> int: - return 0 +def solve_part_2(data: List[str]) -> int: + total = 0 + for chunk in chunks(data, 3): + common_letter = set(chunk[0]).intersection(set(chunk[1])).intersection(set(chunk[2])).pop() + total += string.ascii_letters.index(common_letter) + 1 + return total +def chunks(data, size): + for i in range(0, len(data), size): + yield data[i:i+size] + if __name__ == "__main__": - main("inputs/day03-test1", expected_part_1=157) - main("inputs/day03", expected_part_1=7716) + + main("inputs/day03-test1", expected_part_1=157, expected_part_2=70) + main("inputs/day03", expected_part_1=7716, expected_part_2=2973)