-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.cs
52 lines (46 loc) · 1.57 KB
/
Day1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace Advent_of_Code_2022
{
internal class Day1 : ISolver
{
public string PartOne(string input)
{
ProcessInput(input);
return elfs.Max(e => e.Value.TotalCalories).ToString();
}
public string PartTwo(string input) => elfs.OrderByDescending(e => e.Value.TotalCalories)
.Take(3)
.Sum(e => e.Value.TotalCalories)
.ToString();
public string Title => "Calorie Counting";
private static readonly Dictionary<int, Elf> elfs = new();
private static void ProcessInput(string input)
{
var lines = input.Split('\n');
var elfNumber = 0;
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
{
elfNumber++;
}
else
{
if (!elfs.ContainsKey(elfNumber))
{
elfs[elfNumber] = new Elf();
}
elfs[elfNumber].Items.Add(new Elf.Item() { Calories = int.Parse(line) });
}
}
}
class Elf
{
public List<Item> Items { get; set; } = new List<Item>();
public int TotalCalories => Items.Sum(i => i.Calories);
public class Item
{
public int Calories { get; set; }
}
}
}
}