|
|
@ -1,7 +1,20 @@ |
|
|
|
lines = [i for i in open("input.txt", 'r').read().splitlines()] |
|
|
|
lines = [i for i in open("input.txt", 'r').read().splitlines()] |
|
|
|
|
|
|
|
|
|
|
|
sum = 0 |
|
|
|
sum = 0 |
|
|
|
|
|
|
|
sum2 = 0 |
|
|
|
nonqual = ['0','1','2','3','4','5','6','7','8','9','.'] |
|
|
|
nonqual = ['0','1','2','3','4','5','6','7','8','9','.'] |
|
|
|
|
|
|
|
gears = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def checkValid(x,y): |
|
|
|
|
|
|
|
if lines[x][y] == "*": |
|
|
|
|
|
|
|
coord = str(x) + "," + str(y) |
|
|
|
|
|
|
|
if coord not in gears: |
|
|
|
|
|
|
|
gears[coord] = [int(num)] |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
gears[coord].append(int(num)) |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
if lines[x][y] not in nonqual: |
|
|
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
for i in range(len(lines)): |
|
|
|
for i in range(len(lines)): |
|
|
|
columns = len(lines[i]) |
|
|
|
columns = len(lines[i]) |
|
|
@ -19,19 +32,22 @@ for i in range(len(lines)): |
|
|
|
break |
|
|
|
break |
|
|
|
lennum = len(num) |
|
|
|
lennum = len(num) |
|
|
|
for x in range(0 if j == 0 else j-1, columns if j + lennum == columns else j+lennum+1): |
|
|
|
for x in range(0 if j == 0 else j-1, columns if j + lennum == columns else j+lennum+1): |
|
|
|
if i != 0: |
|
|
|
if i != 0 and checkValid(i-1, x): |
|
|
|
if not lines[i-1][x] in nonqual: |
|
|
|
|
|
|
|
valid = True |
|
|
|
valid = True |
|
|
|
if i != len(lines)-1: |
|
|
|
if i != len(lines)-1 and checkValid(i+1, x): |
|
|
|
if not lines[i+1][x] in nonqual: |
|
|
|
|
|
|
|
valid = True |
|
|
|
valid = True |
|
|
|
if j != 0 and not lines[i][j-1] in nonqual: |
|
|
|
if j != 0 and checkValid(i, j-1): |
|
|
|
valid = True |
|
|
|
valid = True |
|
|
|
if j+lennum != columns and not lines[i][j+lennum] in nonqual: |
|
|
|
if j+lennum != columns and checkValid(i, j+lennum): |
|
|
|
valid = True |
|
|
|
valid = True |
|
|
|
if valid: |
|
|
|
if valid: |
|
|
|
sum += int(num) |
|
|
|
sum += int(num) |
|
|
|
j += lennum |
|
|
|
j += lennum |
|
|
|
j += 1 |
|
|
|
j += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for coord in gears: |
|
|
|
|
|
|
|
if len(gears[coord]) == 2: |
|
|
|
|
|
|
|
sum2 += gears[coord][0] * gears[coord][1] |
|
|
|
|
|
|
|
|
|
|
|
print(sum) |
|
|
|
print(sum) |
|
|
|
|
|
|
|
print(sum2) |