Advertisement
Guest User

aoc/day3

a guest
Dec 16th, 2022
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import string
  2. alphabet = list(string.ascii_lowercase)
  3.  
  4. input = []
  5. with open("day3input.txt", 'r') as f:
  6.     input = f.read()
  7.  
  8. def find_duplicate(line1, line2):
  9.     for a in line1:
  10.         for b in line2:
  11.             if a == b:
  12.                 return a
  13.  
  14. def calc_prio(item):
  15.     if item.islower():
  16.         return alphabet.index(item) + 1
  17.     else: return alphabet.index(item.lower()) + 27
  18.  
  19. def part1():
  20.     prio_sum = 0
  21.     for line in input.split():
  22.         firstpart, secondpart = line[:len(line)//2], line[len(line)//2:]
  23.         prio_sum += calc_prio(find_duplicate(firstpart, secondpart))
  24.     print(prio_sum)
  25.  
  26. def find_common(lines: list):
  27.     for a in alphabet:
  28.         if a in lines[0] and a in lines[1] and a in lines[2]:
  29.             return a
  30.         if a.upper() in lines[0] and a.upper() in lines[1] and a.upper() in lines[2]:
  31.             return a.upper()
  32.  
  33. def part2():
  34.     prio_sum = 0
  35.     group_of_three = []
  36.     for idx, x in enumerate(input.split(), 1):
  37.         group_of_three.append(x)
  38.         if idx % 3 == 0:
  39.             prio_sum += calc_prio(find_common(group_of_three))
  40.             group_of_three.clear()
  41.     print(prio_sum)
  42.  
  43. part1()
  44. part2()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement