C#C
C#3y ago
AceChewy

Advent of Code Day 1

I'm not sure why my code isn't accepted even though I'm sure it works:

private int Sum() 
{
   int Overall = 0;

   Console.WriteLine("Enter the calibration document. Enter an empty line to finish.");

   string line;
   while ((line = Console.ReadLine()) != "")
   {
       int firstDigit = 0;
       int lastDigit = 0;

       // Find the first digit
       for (int i = 0; i < line.Length; i++)
       {
           if (Char.IsDigit(line[i]))
           {
               firstDigit = Int32.Parse(line[i].ToString());
               break;
           }
       }

       // Find the last digit
       for (int i = line.Length - 1; i >= 0; i--)
       {
           if (Char.IsDigit(line[i]))
           {
               lastDigit = Int32.Parse(line[i].ToString());
               break;
           }
       }

       Overall += firstDigit  + lastDigit;
   }

   return Overall;
}
Was this page helpful?