sum-files-challenge icon indicating copy to clipboard operation
sum-files-challenge copied to clipboard

Submission: C#

Open veektorh opened this issue 7 years ago • 0 comments
trafficstars

dotnet core 2.1.302

  • Time : ~ 15s

Hardware :

  Processor Name: Intel Core i5
  Processor Speed: 1.7 GHz
  Number of Processors: 1
  Total Number of Cores: 2
  L2 Cache (per Core):  512KB
  L3 Cache: 3 MB
  Memory: 6 GB

Program

using System;
using System.IO;

namespace SumNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            SumIntegers();

        }

        public static void SumIntegers()
        {
            var startTime = DateTime.Now;

            //replace with directory path
            var dirPath = Directory.GetCurrentDirectory() + @"\files";

            var folders = Directory.GetDirectories(dirPath);
            long total = 0;

            for (int i = 0; i < folders.Length; i++)
            {
                var folder = folders[i];
                var files = Directory.GetFiles(folder);

                for (int j = 0; j < files.Length; j++)
                {
                    var file = files[j];
                    var fileContent = File.ReadAllLines(file);

                    for (int k = 0; k < fileContent.Length; k++)
                    {
                        var numbers = fileContent[k].Split(',');

                        for (int l = 0; l < numbers.Length; l++)
                        {
                            total += Convert.ToInt64(numbers[l]);
                        }
                    }

                }

            }

            Console.WriteLine("total : " + total);
            Console.WriteLine("Milliseconds : " + (DateTime.Now - startTime).TotalMilliseconds);
        }


    }
}


Output

total : 49947871404 Milliseconds : 15210.5908

veektorh avatar Sep 27 '18 14:09 veektorh