Advertisement
illuminati229

AoC 2022 Day 3

Dec 3rd, 2022 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. def priority_score(ltr: str):
  2.     if ltr.isupper():
  3.         return ord(ltr) - 38
  4.     else:
  5.         return ord(ltr) - 96
  6.  
  7.  
  8. def rucksack_inspection(filepath, group=False):
  9.     with open(filepath) as fin:
  10.         rucksacks = fin.read().split('\n')
  11.  
  12.     score = 0
  13.     if not group:
  14.         for rucksack in rucksacks:
  15.             for item in rucksack[:len(rucksack)//2]:
  16.                 if item in rucksack[len(rucksack)//2:]:
  17.                     score += priority_score(item)
  18.                     break
  19.     else:
  20.         for a, b, c in zip(*[iter(rucksacks)] * 3):
  21.             for item in a:
  22.                 if item in b and item in c:
  23.                     score += priority_score(item)
  24.                     break
  25.  
  26.     return score
  27.  
  28.  
  29. def main():
  30.     assert rucksack_inspection('test.txt') == 157
  31.     print(rucksack_inspection('input.txt'))
  32.  
  33.     assert rucksack_inspection('test.txt', True) == 70
  34.     print(rucksack_inspection('input.txt', True))
  35.  
  36.  
  37. if __name__ == '__main__':
  38.     main()
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement