Untitled

 avatar
unknown
csharp
a year ago
3.7 kB
7
Indexable

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;

class AdventDayThree
{
    static void Main(string[] args)
    {
        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "input.txt");

        if (File.Exists(path))
        {
            string[] input = File.ReadAllLines(path);
        //    string[] input = {
        //    "467..114..",
        //    "...*......",
        //    "..35..633.",
        //    "......#...",
        //    "617*......",
        //    ".....+.58.",
        //    "..592.....",
        //    "......755.",
        //    "...$.*....",
        //    ".664.598.."
        //};
            int suma = CalcularSumaPartes(input);
            Console.WriteLine("Suma total: " + suma);
        }
        else
        {
            Console.WriteLine("No sabes ni leer un archivo melon");
        }

        
    }

    static int CalcularSumaPartes(string[] matriz)
    {
        int sumaTotal = 0;
        int filas = matriz.Length;
        int columnas = matriz[0].Length;

        // Direcciones: izquierda, derecha, arriba, abajo y cuatro diagonales
        int[] coordenadaX = { -1, 1, 0, 0, -1, -1, 1, 1 };
        int[] coordenadaY = { 0, 0, -1, 1, -1, 1, -1, 1 };

        bool[,] comprobado = new bool[filas, columnas];

        for (int i = 0; i < filas; i++)
        {
            for (int j = 0; j < columnas; j++)
            {
                if (char.IsDigit(matriz[i][j]) && !comprobado[i, j])
                {

                    // Encontrar el número completo y calcular su suma
                    int numero = ObtenerNumero(matriz, comprobado, i, j, coordenadaX, coordenadaY);
                    sumaTotal += numero;
                  
                   
                }
            }
        }

        return sumaTotal;
    }
    
    //busca el numero completo y lo masrca como comprobado
    static int ObtenerNumero(string[] matriz, bool[,] comprobado, int fila, int columna, int[] coordenadaX, int[] coordenadaY)
    {
        int numero = 0;
        int filas = matriz.Length;
        int columnas = matriz[0].Length;

        Stack<(int, int)> pila = new Stack<(int, int)>();
        pila.Push((fila, columna));
        //tiene algun simbolo distinto de . adyacente
        bool esValido = false; 

        while (pila.Count > 0)
        {
            (int f, int c) = pila.Pop();

            if (comprobado[f, c] || !char.IsDigit(matriz[f][c]))
                continue;

            comprobado[f, c] = true;
            numero = numero * 10 + (matriz[f][c] - '0');

            // comprobacion de simbolo adyacente
            for (int d = 0; d < 8; d++)
            {
                int filaAdyacente = f + coordenadaX[d];
                int columnaAdyacente = c + coordenadaY[d];

                if (filaAdyacente >= 0 && filaAdyacente < filas && columnaAdyacente >= 0 && columnaAdyacente < columnas)
                {
                    if (!char.IsDigit(matriz[filaAdyacente][columnaAdyacente]) && matriz[filaAdyacente][columnaAdyacente] != '.')
                    {
                        esValido = true;
                    }
                    else if (char.IsDigit(matriz[filaAdyacente][columnaAdyacente]) && !comprobado[filaAdyacente, columnaAdyacente])
                    {
                        pila.Push((filaAdyacente, columnaAdyacente));
                    }
                }
            }
        }
        return esValido ? numero : 0;
    }
}

Editor is loading...
Leave a Comment