-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.py
100 lines (79 loc) · 2.46 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import regex as re
file = open('1-input.txt')
document = file.readlines()
number_pattern = re.compile(r'one|two|three|four|five|six|seven|eight|nine|\d+')
number_pattern_digit_only = re.compile(r'\d+')
total1 = 0
total2 = 0
numbdic = {
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9'
}
numbdic2 = {
'one': 'o1e',
'two': 't2o',
'three': 't3e',
'four': 'f4r',
'five': 'f5e',
'six': 's6x',
'seven': 's7n',
'eight': 'e8t',
'nine': 'n9e'
}
for line in document:
line = line.replace('\n', '')
line2 = '' + line
print('------------------------------')
print('input: ', line)
# Find all numbers text and digit
for repl in numbdic2:
line2 = line2.replace(repl, numbdic2[repl])
#numbers1 = number_pattern.findall(line, overlapped = True)
numbers1 = number_pattern.findall(line)
numbers2 = number_pattern_digit_only.findall(line2)
print('numbers: ', numbers1)
print('numbers: ', numbers2)
first1 = numbers1[0]
last1 = numbers1[-1]
first2 = numbers2[0]
last2 = numbers2[-1]
# Replace text with digits
if(numbers1[0] in numbdic):
first1 = numbers1[0].replace(numbers1[0], numbdic[numbers1[0]])
else:
first1 = numbers1[0]
if(numbers1[-1] in numbdic):
last1 = numbers1[-1].replace(numbers1[-1], numbdic[numbers1[-1]])
else:
last1 = numbers1[-1]
print('first1: ', numbers1[0])
print('last1: ', numbers1[-1])
print('first2: ', numbers2[0])
print('last2: ', numbers2[-1])
combined1 = first1[0] + last1[-1]
combined2 = first2[0] + last2[-1]
if(combined1 != combined2):
print('----------------- ERROR! ---------------------')
print('----------------- ERROR! ---------------------')
print('----------------- ERROR! ---------------------')
print('----------------- ERROR! ---------------------')
print('----------------- ERROR! ---------------------')
print('----------------- ERROR! ---------------------')
print('input: ', line)
print('combined1', combined1)
print('combined2', combined2)
print('total1 before: ', total1)
print('total2 before: ', total2)
total1 += int(combined1)
total2 += int(combined2)
print('total1 after: ', total1)
print('total2 after: ', total2)
print(total1)
print(total2)