Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def get_puzzle_data(a_file_name: str) -> list[str]:
- with open(a_file_name) as INFILE:
- return [line.rstrip() for line in INFILE]
- def solve_p1(a_puzzle_data: list[str]) -> int:
- ret: int = 0
- answers: str = ''
- for form in a_puzzle_data:
- if form == '':
- ret += len(set(answers))
- answers = ''
- else:
- answers += form
- if answers:
- ret += len(set(answers))
- return ret
- def solve_p2(a_puzzle_data: list[str]) -> int:
- p2_score: int = 0
- answers: list[str] = []
- def score_p2_group(a_answers: list[str]) -> int:
- group_score: int = 0
- common_answers: set[str] = set(a_answers[0])
- if len(a_answers) == 1:
- group_score = len(common_answers)
- else:
- for i in a_answers[1:]:
- common_answers = common_answers & set(i)
- group_score += len(common_answers)
- return group_score
- for group_form in a_puzzle_data:
- if group_form == '':
- p2_score += score_p2_group(answers)
- answers = []
- else:
- answers.append(group_form)
- if answers:
- p2_score += score_p2_group(answers)
- return p2_score
- puzzle_data = get_puzzle_data('aoc_2020/aoc_2020_day_06.txt')
- # print(f"{puzzle_data=}")
- print(f"p1={solve_p1(puzzle_data)}")
- print(f"p2={solve_p2(puzzle_data)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement