-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.py
42 lines (31 loc) · 825 Bytes
/
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
def is_password(num):
s = str(num)
adjacent = any(a == b for a, b in zip(s, s[1:]))
monotonic = all(a <= b for a, b in zip(s, s[1:]))
return adjacent and monotonic
def is_password_b(num):
s = str(num)
groups = set()
c = 1
prev = ""
for d in s:
if d == prev:
c += 1
else:
prev = d
groups.add(c)
c = 1
groups.add(c)
two_adjacent = 2 in groups
monotonic = all(a <= b for a, b in zip(s, s[1:]))
return two_adjacent and monotonic
def a(start, stop):
return sum(is_password(i) for i in range(start, stop))
def b(start, stop):
return sum(is_password_b(i) for i in range(start, stop))
def main():
start = 147981
stop = 691423
print(b(start, stop))
if __name__ == "__main__":
main()