finish day2.1, move input reading out of day1

This commit is contained in:
CptCaptain 2021-01-20 19:40:42 +01:00
parent ce766388fd
commit 3de439fa41
4 changed files with 1086 additions and 7 deletions

View File

@ -1,8 +1,9 @@
import pytest
from itertools import combinations from itertools import combinations
from typing import Tuple, List from typing import Tuple, List
from math import prod from math import prod
from utils import read_input_data
def test_find_sum_two(): def test_find_sum_two():
test_string = """ test_string = """
@ -27,12 +28,6 @@ def test_find_sum_three():
assert find_sum(intify_input(test_string), 3) == (979, 366, 675) assert find_sum(intify_input(test_string), 3) == (979, 366, 675)
def read_input_data(path: str) -> str:
with open(path, 'r') as f:
data = f.read()
return data
def find_sum(data: List[int], n_summands: int = 2, target: int = 2020) -> Tuple[int, int]: def find_sum(data: List[int], n_summands: int = 2, target: int = 2020) -> Tuple[int, int]:
for combo in combinations(data, n_summands): for combo in combinations(data, n_summands):
if sum(combo) == target: if sum(combo) == target:

1000
day2/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
from utils import read_input_data
from collections import Counter
from typing import List, Dict, Tuple
class TestClass:
test_data = """1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc
"""
def test_count_valid_passwords(self):
parsed = parse_input_data(self.test_data)
assert count_valid_passwords(parsed) == 2
def test_parse_input_data(self):
parsed = parse_input_data(self.test_data)
assert parsed == [
(
{
'lower_bound': 1,
'upper_bound': 3,
'char': 'a',
},
'abcde'
),
(
{
'lower_bound': 1,
'upper_bound': 3,
'char': 'b',
},
'cdefg'
),
(
{
'lower_bound': 2,
'upper_bound': 9,
'char': 'c',
},
'ccccccccc'
),
]
def parse_input_data(data: str) -> List[Tuple[Dict, str]]:
lines = [line for line in data.split('\n') if line]
parsed_passwords = []
for line in lines:
policy, password = line.split(':')
password = password.strip()
policy_dict = parse_policy(policy)
parsed_passwords.append((policy_dict, password))
return parsed_passwords
def parse_policy(input_string: str) -> Dict[str, int]:
range, char = input_string.split()
lower_bound, upper_bound = range.split('-')
return {
'upper_bound': int(upper_bound),
'lower_bound': int(lower_bound),
'char': char,
}
def verify_password(policy, password) -> bool:
counter = Counter(password)
return policy['lower_bound'] <= counter[policy['char']] <= policy['upper_bound']
def count_valid_passwords(parsed_passwords: List[Tuple[Dict, str]]):
return sum([verify_password(pol, pwd) for pol, pwd in parsed_passwords])
if __name__ == '__main__':
raw_data = read_input_data('input.txt')
parsed_data = parse_input_data(raw_data)
result = count_valid_passwords(parsed_data)
print(f'There are {result} valid Passwords, according to their policies')

4
utils.py Normal file
View File

@ -0,0 +1,4 @@
def read_input_data(path: str) -> str:
with open(path, 'r') as f:
data = f.read()
return data