From 957616ac557fef65e5f77e3cd1cca2602da99ffd Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 1 Dec 2022 15:43:15 +0100 Subject: [PATCH] Update Day1_1 --- AoC2022.Day1_1/Program.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/AoC2022.Day1_1/Program.cs b/AoC2022.Day1_1/Program.cs index c7524ed..6488b25 100644 --- a/AoC2022.Day1_1/Program.cs +++ b/AoC2022.Day1_1/Program.cs @@ -1,11 +1,15 @@ -var input = File.ReadAllText("inputs/1_1.txt").TrimEnd('\n'); -var sInput = input.Split("\n\n"); -var elves = sInput.Select(s => new Elf(s.Split("\n").Select(int.Parse).ToList())); +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()); +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(); - public Elf(List calories) => _calories = calories; + + // .ToList() because .Select() gives back an IEnumerable and not a List + public Elf(IEnumerable calories) => _calories = calories.ToList(); }