parent
152016025b
commit
8bd7804a42
1 changed files with 37 additions and 0 deletions
@ -0,0 +1,37 @@ |
||||
lines = [i for i in open("input.txt", 'r').read().splitlines()] |
||||
|
||||
sum = 0 |
||||
nonqual = ['0','1','2','3','4','5','6','7','8','9','.'] |
||||
|
||||
for i in range(len(lines)): |
||||
columns = len(lines[i]) |
||||
j = 0 |
||||
while j < columns: |
||||
if lines[i][j].isnumeric(): |
||||
num = "" |
||||
valid = False |
||||
for k in range(len(lines[i])): |
||||
if j+k >= columns: |
||||
break |
||||
if lines[i][j+k].isnumeric(): |
||||
num += lines[i][j+k] |
||||
else: |
||||
break |
||||
lennum = len(num) |
||||
for x in range(0 if j == 0 else j-1, columns if j + lennum == columns else j+lennum+1): |
||||
if i != 0: |
||||
if not lines[i-1][x] in nonqual: |
||||
valid = True |
||||
if i != len(lines)-1: |
||||
if not lines[i+1][x] in nonqual: |
||||
valid = True |
||||
if j != 0 and not lines[i][j-1] in nonqual: |
||||
valid = True |
||||
if j+lennum != columns and not lines[i][j+lennum] in nonqual: |
||||
valid = True |
||||
if valid: |
||||
sum += int(num) |
||||
j += lennum |
||||
j += 1 |
||||
|
||||
print(sum) |
Loading…
Reference in new issue