Advertisement
Guest User

AOC day 3

a guest
Dec 14th, 2022
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. from aocd import get_data
  2.  
  3. puzzleInput = get_data(year=2022, day=3).splitlines() # get input for day 3 from aoc
  4.  
  5. p1, p2 = 0, 0 # init
  6.  
  7. # General: Priorities are 1-26 for a-z, 27-52 for A-Z
  8.  
  9. # Part 1
  10. for line in puzzleInput: # loop through each line
  11.     rs1, rs2 = line[0:int(len(line)/2)], line[int(len(line)/2):] # split line in half
  12.     common = [match for match in rs1 if match in rs2][0] # find common letter
  13.     p1 += ord(common) - 38 if common.isupper() else ord(common) - 96 # add priority to p1
  14.  
  15. print(p1) # print part 1 answer
  16.  
  17. # Part 2
  18. numberGroups = len(puzzleInput) / 3 # get number of groups
  19. groups = [[elf for elf in puzzleInput[(i-1)*3:i*3]] for i in range(1, int(numberGroups)+1)] # split puzzle input into groups of 3
  20.  
  21. for group in groups: # loop through each group
  22.     usedLetters = [] # make sure that no double letters are used
  23.     for letter in group[0]: # loop through each letter in first elf's list
  24.         if letter in group[1] and letter in group[2] and letter not in usedLetters: # if letter is in all 3 lists
  25.             usedLetters.append(letter) # add letter to usedLetters so it doesn't get counted twice
  26.             p2 += ord(letter) - 38 if letter.isupper() else ord(letter) - 96 # add priority to p2
  27.  
  28. print(p2) # print part 2 answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement