-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3-p1.py
56 lines (47 loc) · 1.6 KB
/
day3-p1.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
import re
f = open('input.txt', 'r')
s = f.readlines()
numeric = '[1-9][0-9]*'
symbol = '[^.0-9]'
r = 0
for i in range(len(s)):
numIndex = []
prevSym = []
currSym = []
nextSym = []
if i == 0:
for n in re.finditer(numeric,s[i].strip()):
numIndex.append(n)
for sym in re.finditer(symbol,s[i].strip()):
currSym.append(sym.start())
for sym in re.finditer(symbol, s[i+1].strip()):
nextSym.append(sym.start())
elif i == (len(s)-1):
for n in re.finditer(numeric,s[i].strip()):
numIndex.append(n)
for sym in re.finditer(symbol,s[i].strip()):
currSym.append(sym.start())
for sym in re.finditer(symbol, s[i-1].strip()):
prevSym.append(sym.start())
else:
for n in re.finditer(numeric,s[i].strip()):
numIndex.append(n)
for sym in re.finditer(symbol,s[i].strip()):
currSym.append(sym.start())
for sym in re.finditer(symbol, s[i-1].strip()):
prevSym.append(sym.start())
for sym in re.finditer(symbol,s[i+1].strip()):
nextSym.append(sym.start())
#print(prevSym)
#print(currSym)
#print(nextSym)
for e in numIndex:
if e.start()-1 in currSym or e.end() in currSym:
r += int(e.group())
for sym in prevSym:
if sym >= e.start()-1 and sym <= e.end():
r += int(e.group())
for sym in nextSym:
if sym >= e.start()-1 and sym <= e.end():
r += int(e.group())
print(r)