diff --git a/AdventOfCode 2022.sln b/Advent Of Code.sln similarity index 86% rename from AdventOfCode 2022.sln rename to Advent Of Code.sln index 8dd4d33..f3b5c28 100644 --- a/AdventOfCode 2022.sln +++ b/Advent Of Code.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32922.545 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day 01", "Day 01\Day 01.csproj", "{0547286C-CC14-44D9-8D7B-26BAA3377721}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "2022", "Day 01\2022.csproj", "{0547286C-CC14-44D9-8D7B-26BAA3377721}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Day 01/Day 01.csproj b/Day 01/2022.csproj similarity index 86% rename from Day 01/Day 01.csproj rename to Day 01/2022.csproj index de8d289..7b9938e 100644 --- a/Day 01/Day 01.csproj +++ b/Day 01/2022.csproj @@ -9,7 +9,7 @@ - + Always diff --git a/Day 01/Day 01/Day01.cs b/Day 01/Day 01/Day01.cs new file mode 100644 index 0000000..e3ef23f --- /dev/null +++ b/Day 01/Day 01/Day01.cs @@ -0,0 +1,29 @@ +namespace Day_01.Day_01 +{ + internal class Day01 + { + private static string inputPath = "Day 01/day-01-input.txt"; + + internal static int GetPart1() + { + return File.ReadAllText(inputPath) + .TrimEnd() + .Split(Environment.NewLine + Environment.NewLine) + .Select(s => s.Split(Environment.NewLine) + .Sum(Convert.ToInt32)) + .MaxBy(v => v); + } + + internal static int GetPart2() + { + return File.ReadAllText(inputPath) + .TrimEnd() + .Split(Environment.NewLine + Environment.NewLine) + .Select(s => s.Split(Environment.NewLine) + .Sum(Convert.ToInt32)) + .OrderByDescending(v => v) + .Take(3) + .Sum(); + } + } +} diff --git a/Day 01/day-01-input.txt b/Day 01/Day 01/day-01-input.txt similarity index 100% rename from Day 01/day-01-input.txt rename to Day 01/Day 01/day-01-input.txt diff --git a/Day 01/Program.cs b/Day 01/Program.cs index 6fe623e..bc0c55a 100644 --- a/Day 01/Program.cs +++ b/Day 01/Program.cs @@ -1,44 +1,4 @@ -string[] data = await File.ReadAllLinesAsync("day-01-input.txt"); +using Day_01.Day_01; - -Console.WriteLine($"Max value: {GetPart2(data)}"); -Console.ReadKey(true); - -int GetPart1(string[] data) -{ - int maxValue = 0, buffer = 0; - - foreach (string value in data) - { - if (string.IsNullOrEmpty(value)) - { - if (buffer > maxValue) maxValue = buffer; - buffer = 0; - continue; - } - - buffer += Convert.ToInt32(value); - } - - return maxValue; -} - -int GetPart2(string[] data) -{ - List values = new List(); - int buffer = 0; - - foreach (string value in data) - { - if (string.IsNullOrEmpty(value)) - { - values.Add(buffer); - buffer = 0; - continue; - } - - buffer += Convert.ToInt32(value); - } - - return values.OrderByDescending(v => v).Take(3).Sum(); -} \ No newline at end of file +Console.WriteLine($"Max value: {Day01.GetPart2()}"); +Console.ReadKey(true); \ No newline at end of file