Guest User

aoc_2022_day_03

a guest
Dec 3rd, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. For example, suppose you have the following list of contents from six rucksacks:
  4. The first rucksack contains the items vJrwpWtwJgWrhcsFMMfFFhFp, which means its first compartment contains the items vJrwpWtwJgWr, while the second compartment contains the items hcsFMMfFFhFp. The only item type that appears in both compartments is lowercase p.
  5. The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only item type that appears in both compartments is uppercase L.
  6. The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item type is uppercase P.
  7. The fourth rucksack's compartments only share item type v.
  8. The fifth rucksack's compartments only share item type t.
  9. The sixth rucksack's compartments only share item type s.
  10.  
  11. To help prioritize item rearrangement, every item type can be converted to a priority:
  12.  
  13. Lowercase item types a through z have priorities 1 through 26.
  14. Uppercase item types A through Z have priorities 27 through 52.
  15. In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (p), 38 (L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is 157.
  16. """
  17.  
  18. import sys
  19. from pathlib import Path
  20. import string
  21.  
  22. LETTERS = string.ascii_letters
  23.  
  24. INPUTS_DIR = 'inputs'
  25. INPUT_FILE = 'day03.txt'
  26. PROJECT_DIR = Path('.')
  27. FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE
  28.  
  29. def read_data(file_path):
  30.     """Read file"""
  31.     for row in open(file_path):
  32.         yield row.strip()
  33.  
  34.  
  35. def sum_result(file_path):
  36.     group = []
  37.     _sum_1 = 0
  38.     _sum_2 = 0
  39.     for s in read_data(file_path):
  40.         item = check_error(s)
  41.         priority = get_priority(item)
  42.         _sum_1 += priority
  43.         group.append(s)
  44.         if len(group) == 3:
  45.             badge = check_badge(*group)
  46.             badge_priority = get_priority(badge)
  47.             _sum_2 += badge_priority
  48.             group.clear()
  49.     return _sum_1, _sum_2
  50.  
  51. def check_error(s):
  52.     s1, s2 = set(s[:len(s) // 2]), set(s[len(s) // 2: ])
  53.     return list(s1.intersection(s2))[0]
  54.  
  55. def check_badge(s1, s2, s3):
  56.     return list(set(s1).intersection(set(s2)).intersection(set(s3)))[0]
  57.  
  58. def get_priority(l):
  59.     """
  60.    Lowercase item types a through z have priorities 1 through 26.
  61.    Uppercase item types A through Z have priorities 27 through 52.
  62.    """
  63.     return LETTERS.index(l) + 1
  64.  
  65. examples = [
  66.     'vJrwpWtwJgWrhcsFMMfFFhFp',
  67.     'jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL',
  68.     'PmmdzqPrVvPwwTWBwg',
  69.     'wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn',
  70.     'ttgJtRGJQctTZtZT',
  71.     'CrZsJsPPZsGzwwsLwLmpwMDw'
  72. ]
  73.  
  74. _sum_1 = 0
  75. _sum_2 = 0
  76. group = []
  77. for s in examples:
  78.     l = check_error(s)
  79.     p = get_priority(l)
  80.     _sum_1 += p
  81.     group.append(s)
  82.     if len(group) == 3:
  83.         b = check_badge(group[0], group[1], group[2])
  84.         print(b)
  85.         p = get_priority(b)
  86.         _sum_2 += p
  87.         group.clear()
  88. print(_sum_1, _sum_2)
  89.  
  90. if __name__ == '__main__':
  91.     FILE_PATH = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH
  92.     print(sum_result(FILE_PATH))
  93.  
Advertisement
Add Comment
Please, Sign In to add comment