58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import re
|
|
|
|
SYMBOLS = ['#', '$', '%', '&', '*', '+', '-', '/', '=', '@']
|
|
|
|
find_number = re.compile(r'\d+')
|
|
|
|
with open('adv03a.txt') as f:
|
|
txt = [l for l in f.readlines() if l]
|
|
sym = [[c in SYMBOLS for c in l] for l in txt]
|
|
matches = [list(find_number.finditer(l)) for l in txt]
|
|
|
|
def num(match):
|
|
#print(match.group(0))
|
|
return int(match.group(0))
|
|
|
|
total = 0
|
|
for linenum in range(len(matches)):
|
|
for match in matches[linenum]:
|
|
has_left = match.start() > 0
|
|
has_right = match.end() < len(txt[linenum])
|
|
has_up = linenum > 0
|
|
has_down = linenum < len(matches) - 1
|
|
if has_up:
|
|
if any(sym[linenum-1][match.start():match.end()]):
|
|
total += num(match)
|
|
continue
|
|
if has_left:
|
|
if sym[linenum-1][match.start()-1]:
|
|
total += num(match)
|
|
continue
|
|
if has_right:
|
|
if sym[linenum-1][match.end()]:
|
|
total += num(match)
|
|
continue
|
|
if has_down:
|
|
if any(sym[linenum+1][match.start():match.end()]):
|
|
total += num(match)
|
|
continue
|
|
if has_left:
|
|
if sym[linenum+1][match.start()-1]:
|
|
total += num(match)
|
|
continue
|
|
if has_right:
|
|
if sym[linenum+1][match.end()]:
|
|
total += num(match)
|
|
continue
|
|
if has_left:
|
|
if sym[linenum][match.start()-1]:
|
|
total += num(match)
|
|
continue
|
|
if has_right:
|
|
num(match)
|
|
if sym[linenum][match.end()]:
|
|
total += num(match)
|
|
continue
|
|
|
|
print(total)
|