-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
47 lines (35 loc) · 1.44 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
from collections import Counter
class Day02:
def __init__(self):
self.puzzle_input = self.load_input()
def part_one(self) -> int:
valid_passwords = 0
for line in self.puzzle_input:
policy, letter, password = map(line.get, ['policy', 'letter', 'password'])
collection = Counter(password)
low, top = map(int, policy.split("-"))
if low <= collection[letter] <= top:
valid_passwords += 1
return valid_passwords
def part_two(self) -> int:
valid_passwords = 0
for line in self.puzzle_input:
policy, letter, password = map(line.get, ['policy', 'letter', 'password'])
low, top = map(int, policy.split("-"))
first_char = password[low - 1]
second_char = password[top - 1]
if first_char != second_char:
if first_char == letter or second_char == letter:
valid_passwords += 1
return valid_passwords
@staticmethod
def load_input() -> list:
list_input = []
with open("input.txt") as file:
for line in file:
policy, letter, password = line.replace(":", "").split()
list_input.append({'policy': policy, 'letter': letter, 'password': password})
return list_input
day = Day02()
print(f'Result part 1: {day.part_one()}')
print(f'Result part 2: {day.part_two()}')