-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday4.py
55 lines (39 loc) · 1.23 KB
/
day4.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
from collections import Counter
def has_2_or_more_matching(pw_str: str) -> bool:
for i in range(5):
if pw_str[i] == pw_str[i + 1]:
return True
return False
def has_exactly_2_matching(pw_str: str) -> bool:
counts = Counter(pw_str)
for i in counts:
if counts[i] == 2:
return True
return False
def part1(range_from: int, range_to: int) -> int:
num_passwords = 0
for password in range(range_from, range_to + 1):
pw_str = str(password)
if not pw_str == ''.join(sorted(pw_str)):
continue
if not has_2_or_more_matching(pw_str):
continue
num_passwords += 1
return num_passwords
def part2(range_from: int, range_to: int) -> int:
num_passwords = 0
for password in range(range_from, range_to + 1):
pw_str = str(password)
if not pw_str == ''.join(sorted(pw_str)):
continue
if not has_exactly_2_matching(pw_str):
continue
num_passwords += 1
return num_passwords
def main():
range_from = 124075
range_to = 580769
print(f'Part 1: {part1(range_from, range_to)}')
print(f'Part 2: {part2(range_from, range_to)}')
if __name__ == "__main__":
main()