Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- alphabet = list(string.ascii_lowercase)
- input = []
- with open("day3input.txt", 'r') as f:
- input = f.read()
- def find_duplicate(line1, line2):
- for a in line1:
- for b in line2:
- if a == b:
- return a
- def calc_prio(item):
- if item.islower():
- return alphabet.index(item) + 1
- else: return alphabet.index(item.lower()) + 27
- def part1():
- prio_sum = 0
- for line in input.split():
- firstpart, secondpart = line[:len(line)//2], line[len(line)//2:]
- prio_sum += calc_prio(find_duplicate(firstpart, secondpart))
- print(prio_sum)
- def find_common(lines: list):
- for a in alphabet:
- if a in lines[0] and a in lines[1] and a in lines[2]:
- return a
- if a.upper() in lines[0] and a.upper() in lines[1] and a.upper() in lines[2]:
- return a.upper()
- def part2():
- prio_sum = 0
- group_of_three = []
- for idx, x in enumerate(input.split(), 1):
- group_of_three.append(x)
- if idx % 3 == 0:
- prio_sum += calc_prio(find_common(group_of_three))
- group_of_three.clear()
- print(prio_sum)
- part1()
- part2()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement