• # Bonne idée.

    Posté par (site web personnel) . En réponse au message Advent of Code 2023 : Day 3. Évalué à 2. Dernière modification le 04 décembre 2023 à 08:27.

    moi c'est en python, mais c'est une NON réponse... il doit me manquer des cas que je n'identifie pas...

     def nettoyer_point(chaine) :
     """recupère les symboles à utiliser autres que '.'"""
     chaine = chaine.replace(".", "")
     for i in [i for i in range(10)] : 
     chaine = chaine.replace(str(i), '')
     liste = []
     for carac in chaine : 
     if not carac in liste : 
     liste.append(carac)
     return liste
     print(nettoyer_point(computer2))
     def est_voisin_symbol(matrice_, x,y) : 
     "renvoie si la case x, y est voisine d'un symbole"
     matrice = matrice_.copy()
     for i in range(-1,2) : 
     for j in range(-1, 2) : 
     matrice
     if 0<=x+i and 0<=y+j and not (i==j==0): 
     try : 
     #if not(matrice[x][y]=='.' or matrice[x][y].isdigit()):
     if matrice[x+i][y+j] in ['*', '&', '/', '@', '=', '+', '#', '$', '%', '-']: 
     return True, matrice[x+i][y+j]
     except IndexError : 
     pass
     return False, None
     matrice =[]
     for ligne in computer2.split('\n'):
     #print(ligne)
     ligne_ = []
     for carac in ligne : 
     ligne_.append(carac)
     matrice.append(ligne_)
     def verifie_chaine(matrice, chaine,x,y) : 
     "parcours la chaine de gauche à droite jusqu'a un espace"
     x, y = x, y-1
     for _ in range(len(chaine)) : 
     result, symbol = est_voisin_symbol(matrice, x, y)
     if result : 
     return True, symbol
     y-=1
     return False, None
     i=0
     somme = 0
     while i<len(matrice) : 
     j= 0
     nombre=""
     while j<len(matrice[0]) : 
     if matrice[i][j].isdigit() and j!=len(matrice[0])-1 : 
     nombre += matrice[i][j]
     else :
     #gestion dernier sympbol : 
     if j==len(matrice[0])-1 :
     if matrice[i][j].isdigit() : 
     nombre += matrice[i][j]
     result, symbol = verifie_chaine(matrice, nombre, i, j)
     if result : 
     #print(nombre, symbol)
     somme +=int(nombre)
     else :
     if nombre!='' : 
     print('NON', nombre)
     nombre =""
     j+=1
     i+=1