-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.py
29 lines (22 loc) · 807 Bytes
/
02.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
import re
def process_line(line: str) -> bool:
minX, maxX = line.split(' ', 1)[0].split('-')
c = line.split(':', 1)[0][-1]
pwd = line.rsplit(' ', 1)[-1]
return check_pwd(pwd, int(minX), int(maxX), c)
def check_pwd(pwd, minX, maxX, c) -> bool:
m = re.findall(rf'{c}', pwd)
return minX <= len(m) <= maxX
def part1(data):
valid = list(filter(process_line, data.splitlines()))
return len(valid)
def process_line_2(line: str) -> bool:
minX, maxX = line.split(' ', 1)[0].split('-')
c = line.split(':', 1)[0][-1]
pwd = line.rsplit(' ', 1)[-1]
return check_pwd_2(pwd, int(minX), int(maxX), c)
def check_pwd_2(pwd, p1, p2, c) -> bool:
return ((pwd[p1 - 1] == c) + (pwd[p2 - 1] == c)) == 1
def part2(data):
valid = list(filter(process_line_2, data.splitlines()))
return len(valid)