2023 day 3 part 1 solved

kwout 11 months ago
parent d1a0dfae3c
commit 7dfaee0a71
  1. 37
      2023/3.py

@ -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…
Cancel
Save