AdventOfCode/Year2022/Day1.cs

41 lines
988 B
C#
Raw Permalink Normal View History

2023-06-07 21:56:41 +00:00
namespace AdventOfCode.Year2022;
public class Day1
{
2023-06-07 23:12:01 +00:00
private static readonly List<int> CaloriesPerElf = GetCaloriesPerElf();
2023-06-24 21:26:39 +00:00
2023-06-07 21:56:41 +00:00
public Day1()
{
2023-06-24 21:26:39 +00:00
Console.WriteLine($@"
Day1 Solution
Part1 Result: {Part1()}
Part2 Result: {Part2()}
=============================");
2023-06-07 21:56:41 +00:00
}
private static int Part1()
{
2023-06-07 23:12:01 +00:00
return CaloriesPerElf.Max();
2023-06-07 21:56:41 +00:00
}
private static int Part2()
{
2023-06-24 21:26:39 +00:00
return CaloriesPerElf.Order().Reverse().ToArray()[..3].Sum();
2023-06-07 21:56:41 +00:00
}
2023-06-07 23:12:01 +00:00
private static List<int> GetCaloriesPerElf()
2023-06-07 21:56:41 +00:00
{
2023-06-24 21:26:39 +00:00
var calories = File.ReadAllLines("inputs/day1.txt");
var caloriesPerDay = new int[calories.Length];
2023-06-07 21:56:41 +00:00
var index = 0;
2023-06-24 21:26:39 +00:00
foreach (var calorie in calories)
2023-06-07 21:56:41 +00:00
if (calorie == "")
index++;
else
caloriesPerDay[index] += Convert.ToInt32(calorie);
2023-06-24 21:26:39 +00:00
var elfCount = caloriesPerDay.ToList().IndexOf(0);
2023-06-07 23:12:01 +00:00
return caloriesPerDay.ToList().GetRange(0, elfCount);
2023-06-07 21:56:41 +00:00
}
}