-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.py
64 lines (60 loc) · 1.3 KB
/
1.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
60
61
62
63
64
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
input_value = open("input.txt", "r").read()
lines = input_value.split("\n")
numbers = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"zero": 0,
}
total = 0
for line in lines:
item = ""
i = 0
while i < len(line):
char = line[i]
try:
item += str(int(char))
break
except:
pass
found = False
for number in numbers:
if line[i : i + len(number)] == number:
item += str(numbers[number])
i += len(number)
found = True
break
if not found:
i += 1
else:
break
i = 0
while i < len(line[::-1]):
char = line[::-1][i]
try:
item += str(int(char))
break
except:
pass
found = False
for number in numbers:
if line[::-1][i : i + len(number)] == number[::-1]:
item += str(numbers[number])
i += len(number)
found = True
break
if not found:
i += 1
else:
break
total += int(item)
print(total)