AoC2022/AoC2022.Day1/Program.cs

17 lines
1.1 KiB
C#

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<int> _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<int> calories) => _calories = calories.ToList();
}