-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd02.py
More file actions
47 lines (33 loc) · 1.05 KB
/
d02.py
File metadata and controls
47 lines (33 loc) · 1.05 KB
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
import itertools
import fileinput
from collections import Counter
from typing import List
from functools import reduce
def get_input():
data = [l for l in fileinput.input()]
return data
def parse_str(line):
policy, letter, pwd = line.split()
letter = letter[0]
pol_min, pol_max = [int(i) for i in policy.split('-')]
return pol_min, pol_max, letter, pwd
def part1(data: List[str]):
def is_valid(line):
pol_min, pol_max, letter, pwd = parse_str(line)
letter_count = Counter(pwd)
is_valid = pol_min <= letter_count.get(letter, -1) <= pol_max
return int(is_valid)
return sum(is_valid(s) for s in data)
def part2(data):
def is_valid(line):
pol_min, pol_max, letter, pwd = parse_str(line)
pwd = " " + pwd
is_valid = (letter == pwd[pol_min]) ^ (letter == pwd[pol_max])
return int(is_valid)
return sum(is_valid(s) for s in data)
if __name__ == '__main__':
indata = get_input()
res1 = part1(indata)
res2 = part2(indata)
print(res1)
print(res2)