-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
42 lines (27 loc) · 852 Bytes
/
day2.py
File metadata and controls
42 lines (27 loc) · 852 Bytes
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
def Part2isInvalid(num: int) -> bool:
text_version = str(num)
for i in range(len(text_version)):
if i * text_version.count(text_version[0:i]) == len(text_version):
return True
return False
def Part1isInvalid(num: int) -> bool:
stringver = str(num)
if len(stringver) % 2 == 1:
return False
half_mark = (len(stringver)//2)
first_half = stringver[0:half_mark]
second_half = stringver[half_mark:]
return first_half == second_half
numset = ""
with open('day2_ranges.txt') as file:
numset = file.read()
num_ranges = numset.split(',')
total = 0
for rng in num_ranges:
this_range = rng.split('-')
bottom = int(this_range[0])
top = int(this_range[1])
for i in range(bottom, top):
if Part2isInvalid(i):
total += i
print(total)