Advertisement
bob_f

AOC2020D06.py

Sep 25th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1.  
  2. import re
  3.  
  4. def get_puzzle_data(a_file_name: str) -> list[str]:
  5.     with open(a_file_name) as INFILE:
  6.         return [line.rstrip() for line in INFILE]
  7.  
  8. def solve_p1(a_puzzle_data: list[str]) -> int:
  9.     ret: int = 0
  10.     answers: str = ''
  11.  
  12.     for form in a_puzzle_data:
  13.         if form == '':
  14.             ret += len(set(answers))
  15.             answers = ''
  16.         else:
  17.             answers += form
  18.  
  19.     if answers:
  20.         ret += len(set(answers))
  21.  
  22.     return ret
  23.  
  24. def solve_p2(a_puzzle_data: list[str]) -> int:
  25.     p2_score: int = 0
  26.     answers: list[str] = []
  27.  
  28.     def score_p2_group(a_answers: list[str]) -> int:
  29.         group_score: int = 0
  30.         common_answers: set[str] = set(a_answers[0])
  31.  
  32.         if len(a_answers) == 1:
  33.             group_score = len(common_answers)
  34.         else:
  35.             for i in a_answers[1:]:
  36.                 common_answers = common_answers & set(i)
  37.  
  38.             group_score += len(common_answers)
  39.  
  40.         return group_score
  41.  
  42.     for group_form in a_puzzle_data:
  43.         if group_form == '':
  44.             p2_score += score_p2_group(answers)
  45.             answers = []
  46.         else:
  47.             answers.append(group_form)
  48.  
  49.     if answers:
  50.         p2_score += score_p2_group(answers)
  51.  
  52.     return p2_score
  53.  
  54. puzzle_data = get_puzzle_data('aoc_2020/aoc_2020_day_06.txt')
  55. # print(f"{puzzle_data=}")
  56.  
  57. print(f"p1={solve_p1(puzzle_data)}")
  58. print(f"p2={solve_p2(puzzle_data)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement