2022 day 3 complete

This commit is contained in:
Jake Pullen
2023-12-02 18:19:16 +00:00
parent c2d362c6b3
commit ff9d1a7909
+17 -2
View File
@@ -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)
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)