-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
60 lines (41 loc) · 1.39 KB
/
day04.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
56
57
58
59
from util import Day
from itertools import groupby
def check_password(password: str, limit_groups=False) -> bool:
"""Password Checker
Arguments:
password {str} -- 6 string password
Keyword Arguments:
limit_groups {bool} -- Only conut real doubles (no groups) (default: {False})
Returns:
bool -- Valid password
"""
password = password.strip() # because always
six = len(password) == 6
# doub = not sorted(set(password)) == sorted(password) # Part 1 worked
for _, g in groupby(password):
lg = len(list(g)) # This here killed me. g is empty after one run.
if lg >= 2 and not limit_groups:
doub = True
break
elif lg == 2 and limit_groups:
doub = True
break
else:
doub = False
sort = "".join(sorted(password)) == password
return six and doub and sort
if __name__ == "__main__":
# Part 1
part1 = Day(4, 1)
part1.load(typing=int, sep="-")
part1.load(list(range(part1.data[0], part1.data[1] + 1)))
part1.apply(str)
part1.apply(check_password)
print(part1.answer(part1.sum()))
# Part 2
part2 = Day(4, 2)
part2.load(typing=int, sep="-")
part2.load(list(range(part2.data[0], part2.data[1] + 1)))
part2.apply(str)
part2.apply(check_password, limit_groups=True)
print(part2.answer(part2.sum()))