AdventOfCode2022/Handlers/Day01.cs

36 lines
951 B
C#
Raw Normal View History

2022-12-02 16:46:35 +01:00
namespace AdventOfCode2022.Handlers
{
public class Day01
{
string[] elves;
IEnumerable<int> caloriesPerElf;
public Day01()
{
var lines = System.IO.File.ReadAllText(@"./days/01/input");
elves = lines.Split("\n\n");
caloriesPerElf = elves.Select(ParseCalories);
}
private int ParseCalories(string elf)
{
return elf.Split("\n")
.Where(calorie => !string.IsNullOrWhiteSpace(calorie))
.Select(int.Parse)
.Sum();
}
public void PartA()
{
var max = caloriesPerElf.Max();
Console.WriteLine($"Highest calories: {max}");
}
public void PartB()
{
var topThree = caloriesPerElf.OrderByDescending(calories => calories).Take(3).Sum();
Console.WriteLine($"Top three calories: {topThree}");
}
}
}