-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04_solve_puzzle2.py
59 lines (46 loc) · 1.95 KB
/
day04_solve_puzzle2.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
def is_ascending(number):
ns = str(number)
return all([ns[i] >= ns[i-1] for i in range(1, len(ns))])
assert is_ascending(123456) == True
assert is_ascending(1234444456) == True
assert is_ascending(1234566666) == True
assert is_ascending(1111234566666) == True
assert is_ascending(1234566566) == False
assert is_ascending(112211) == False
def has_lonely_pair(number):
ns = "x" + str(number) + "x"
lonely_pairs = []
for i in range(1,len(ns)-2):
lonely_pairs.append((ns[i-1] != ns[i]) and (ns[i] == ns[i+1]) and (ns[i+1] != ns[i+2]))
return any(lonely_pairs)
assert has_lonely_pair(112233) == True
assert has_lonely_pair(132213) == True
assert has_lonely_pair(111233) == True
assert has_lonely_pair(123433) == True
assert has_lonely_pair(111234) == False
assert has_lonely_pair(111222) == False
assert has_lonely_pair(123444) == False
def check_exanded_rules(password):
return is_ascending(password) and has_lonely_pair(password)
def count_valid_passwords(_min, _max):
""" The min/max values ensures that the length and the range is within the
password definition, and there is no need to check for it explicitly """
return sum([check_exanded_rules(password) for password in range(_min, _max+1)])
# Asserts from problem description
assert check_exanded_rules(112233) == True
assert check_exanded_rules(123444) == False
assert check_exanded_rules(111122) == True
assert check_exanded_rules(223450) == False
assert check_exanded_rules(123789) == False
# These are asserts are "hand calculated"
assert check_exanded_rules(111123) == False
assert check_exanded_rules(122333) == True
assert check_exanded_rules(111111) == False
assert check_exanded_rules(123788) == True
assert check_exanded_rules(889999) == True
assert check_exanded_rules(112345) == True
assert check_exanded_rules(122345) == True
assert check_exanded_rules(112311) == False
assert check_exanded_rules(111111) == False
assert check_exanded_rules(123456) == False
print(count_valid_passwords(146810, 612564))