-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
46 lines (40 loc) · 1.16 KB
/
part2.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
# with open("example2", "r") as f:
with open("input", "r") as f:
lines = [l.strip() for l in f.readlines()]
nums = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
def is_num(s):
return s.isdigit() or s in nums
res = 0
for line in lines:
found = False
first_num = None
for i in range(len(line)):
if found:
break
if is_num(line[i]):
first_num = line[i]
found = True
break
for num in nums:
candidate = line[i : i + len(num)]
if is_num(candidate):
first_num = str(nums.index(candidate) + 1)
found = True
break
found = False
last_num = None
for i in range(len(line) - 1, -1, -1):
if found:
break
if is_num(line[i]):
last_num = line[i]
found = True
break
for num in nums:
candidate = line[i - len(num) + 1 : i + 1]
if is_num(candidate):
last_num = str(nums.index(candidate) + 1)
found = True
break
res += int(first_num + last_num)
print(res)