diff --git a/AoC2022.Day1_1/AoC2022.Day1_1.csproj b/AoC2022.Day1/AoC2022.Day1.csproj similarity index 100% rename from AoC2022.Day1_1/AoC2022.Day1_1.csproj rename to AoC2022.Day1/AoC2022.Day1.csproj diff --git a/AoC2022.Day1/Program.cs b/AoC2022.Day1/Program.cs new file mode 100644 index 0000000..7e36411 --- /dev/null +++ b/AoC2022.Day1/Program.cs @@ -0,0 +1,16 @@ +var input = File.ReadAllText("../../../../inputs/1_1.txt").TrimEnd('\n').Split("\n\n"); // Read input file, remove trailing newlines, split by double newline +var elves = input.Select(s => new Elf(s.Split("\n").Select(int.Parse))); // Use LINQ magic (select) to transform the collection of strings into a collection of Elves + +Console.WriteLine(elves.MaxBy(elf => elf.Total())!.Total()); // Output Elf with highest total number of calories +Console.WriteLine(elves.OrderByDescending(elf => elf.Total()).Take(3).Sum(elf => elf.Total())); // Output total calories carried by top 3 elves + +internal class Elf { + // Elves have a list of calories (technically a list of consumables that each have a calorie value but that distinction doesn't appear to be relevant yet + private readonly List _calories; + + // LINQ shenanigans to easily get the sum of these calorie values + public int Total() => _calories.Sum(); + + // .ToList() because .Select() gives back an IEnumerable and not a List + public Elf(IEnumerable calories) => _calories = calories.ToList(); +} diff --git a/AoC2022.Day1_1/Program.cs b/AoC2022.Day1_1/Program.cs deleted file mode 100644 index 6488b25..0000000 --- a/AoC2022.Day1_1/Program.cs +++ /dev/null @@ -1,15 +0,0 @@ -var input = File.ReadAllText("inputs/1_1.txt").TrimEnd('\n').Split("\n\n"); // Read input file, remove trailing newlines, split by double newline -var elves = input.Select(s => new Elf(s.Split("\n").Select(int.Parse))); // Use LINQ magic (select) to transform the collection of strings into a collection of Elves - -Console.WriteLine(elves.MaxBy(elf => elf.Total())!.Total()); // Output Elf with highest total number of calories - -internal class Elf { - // Elves have a list of calories (technically a list of consumables that each have a calorie value but that distinction doesn't appear to be relevant yet - private readonly List _calories; - - // LINQ shenanigans to easily get the sum of these calorie values - public int Total() => _calories.Sum(); - - // .ToList() because .Select() gives back an IEnumerable and not a List - public Elf(IEnumerable calories) => _calories = calories.ToList(); -} diff --git a/AoC2022.sln b/AoC2022.sln index 9e16274..7a497df 100644 --- a/AoC2022.sln +++ b/AoC2022.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AoC2022.Day1_1", "AoC2022.Day1_1\AoC2022.Day1_1.csproj", "{620896E5-2412-4713-BA7B-ABB66875D3D6}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AoC2022.Day1", "AoC2022.Day1\AoC2022.Day1.csproj", "{620896E5-2412-4713-BA7B-ABB66875D3D6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution