aoc2023/d03/run1.py

37 lines
1.1 KiB
Python
Raw Normal View History

2023-12-03 07:20:28 +00:00
import sys
myList = list(list(map(str,s)) for s in sys.stdin.read().splitlines())
def if_symbol_adj(l, n, i):
for m in [n-1, n, n+1]:
if m < 0 or m > len(l)-1: continue
for j in [i-1, i, i+1]:
2023-12-03 07:41:13 +00:00
if j < 0 or j > len(l[m])-1: continue
2023-12-03 07:20:28 +00:00
if l[m][j] != "." and l[m][j].isdigit() is False:
return True
else:
pass
return False
def main(S = 0):
# de haut en bas
for n in range(len(myList)):
nb = ""
if_sum = False
# de gauche à droite
for i in range(len(myList[n])):
if myList[n][i].isdigit():
nb = "".join([nb, myList[n][i]])
if_sum = if_sum or if_symbol_adj(myList, n, i)
2023-12-03 07:41:13 +00:00
if not myList[n][i].isdigit() or i == len(myList[n]) -1:
# soit pas encore un nombre soit la fin d'un nombre, soit la fin d'une ligne
#if nb!="": print(nb, if_sum)
S += (int(nb) if nb != "" and if_sum else 0)
2023-12-03 07:20:28 +00:00
if_sum = False
nb = ""
return S
if __name__ == '__main__':
print(main())