AdventOfCode/Year2022/Day1.cs

48 lines
1.2 KiB
C#
Raw 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-07 21:56:41 +00:00
public Day1()
{
2023-06-07 22:59:16 +00:00
Console.WriteLine("\nDay1 Solution");
Console.WriteLine($"Part1 Result: {Part1()}");
Console.WriteLine($"Part2 Result: {Part2()}");
Console.WriteLine("\n=============================\n");
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()
{
List<int> top3 = new List<int>();
for (int i = 0; i < 3; i++)
{
2023-06-07 23:12:01 +00:00
top3.Add(CaloriesPerElf.Max());
CaloriesPerElf.Remove(CaloriesPerElf.Max());
2023-06-07 21:56:41 +00:00
}
return top3.Sum();
}
2023-06-07 23:12:01 +00:00
private static List<int> GetCaloriesPerElf()
2023-06-07 21:56:41 +00:00
{
string[] calories = File.ReadAllLines("inputs/day1.txt");
int[] caloriesPerDay = new int[calories.Length];
var index = 0;
foreach (string calorie in calories)
{
if (calorie == "")
index++;
else
caloriesPerDay[index] += Convert.ToInt32(calorie);
}
int 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
}
}