diff --git a/2022/day_3/part 2 solution.py b/2022/day_3/part 2 solution.py index 66b2b10..623a95f 100644 --- a/2022/day_3/part 2 solution.py +++ b/2022/day_3/part 2 solution.py @@ -1,4 +1,4 @@ -with open(r'advent_of_code\2022\day_3\input.txt', 'r') as file: +with open(r'D:\repos\advent_of_code\2022\day_3\input.txt', 'r') as file: input = file.read() test_input = '''vJrwpWtwJgWrhcsFMMfFFhFp @@ -19,4 +19,19 @@ def task_1(file): ascii_start = 96 if common_item.islower() else 38 priority += ord(common_item) - ascii_start print("Task 1 result: " + str(priority)) -task_1(input) \ No newline at end of file +task_1(input) + +def task_2(file): + # group 3 lines together, find the common item between all 3 lines + # using ASCII + priority = 0 + #take the input and split it into groups of 3 lines + lines = file.split('\n') + grouped_lines = [lines[i:i + 3] for i in range(0, len(lines), 3)] + for group in grouped_lines: + common_items_set = set(group[0]).intersection(group[1], group[2]) + common_item = common_items_set.pop() + ascii_start = 96 if common_item.islower() else 38 + priority += ord(common_item) - ascii_start + print("Task 2 result: " + str(priority)) +task_2(input) \ No newline at end of file