-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.py
More file actions
56 lines (42 loc) · 1.2 KB
/
day04.py
File metadata and controls
56 lines (42 loc) · 1.2 KB
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
import sys
low, high = list(map(int, sys.stdin.readline().strip().split('-')))
# part 1
def is_valid(val: int):
val = list(map(int, str(val)))
has_same = False
for a, b in zip(val[:-1], val[1:]):
if a > b:
return False
elif a == b:
has_same = True
return has_same
## Tests
# print(is_valid(111123)) # True
# print(is_valid(135679)) # False
# print(is_valid(111111)) # True
# print(is_valid(223450)) # False
# print(is_valid(123789)) # False
res = sum([is_valid(num) for num in range(low, high + 1)])
print(res)
# part 2
def is_valid(val: int):
val = list(map(int, str(val)))
adjacent_counts = []
adjacent_count = 0
for a, b in zip(val[:-1], val[1:]):
if a > b:
return False
elif a == b:
adjacent_count += 1
else:
adjacent_counts.append(adjacent_count)
adjacent_count = 0
adjacent_counts.append(adjacent_count)
adjacent_counts = list(map(lambda x: x + 1, adjacent_counts))
return 2 in adjacent_counts
## Tests
# print(is_valid(112233))
# print(is_valid(123444))
# print(is_valid(111122))
res = sum([is_valid(num) for num in range(low, high + 1)])
print(res)