-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (46 loc) · 1.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from collections import defaultdict
class Day06:
def __init__(self):
self.puzzle_input = self.load_input()
def part_one(self) -> int:
return self.get_answers_group(self.puzzle_input)
def part_two(self) -> int:
return self.get_answers_everyone(self.puzzle_input)
@staticmethod
def get_answers_group(puzzle_input: list) -> int:
answers = 0
for groups in puzzle_input:
groups_set = set()
for group in groups:
groups_set.update(set(group))
answers += len(groups_set)
return answers
@staticmethod
def get_answers_everyone(puzzle_input: list) -> int:
answers = 0
for groups in puzzle_input:
count_letters = defaultdict(int)
for group in groups:
for letter in group:
count_letters[letter] += 1
all_answered = [
key for key, value in count_letters.items() if value == len(groups)
]
answers += len(all_answered)
return answers
@staticmethod
def load_input() -> list:
list_input = []
with open("input.txt") as file:
group = []
for line in file:
if line == "\n":
list_input.append(group)
group = []
else:
group.append(list(line.rstrip()))
list_input.append(group)
return list_input
day = Day06()
print(f"Result part 1: {day.part_one()}")
print(f"Result part 2: {day.part_two()}")