-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3.cs
39 lines (36 loc) · 1.36 KB
/
Day3.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
namespace Advent_of_Code_2022
{
internal class Day3 : ISolver
{
public string Title => "Rucksack Reorganization";
public string PartOne(string input)
{
var lines = input.Split('\n');
var sum = 0;
foreach (var rucksack in lines)
{
var halfway = rucksack.Length / 2;
var compartment1 = rucksack[..halfway];
var compartment2 = rucksack[halfway..];
var sharedItems = compartment1.Intersect(compartment2);
sum += GetPriorities(sharedItems);
}
return sum.ToString();
}
public string PartTwo(string input)
{
var lines = input.Split('\n');
var sum = 0;
for (int i = 0; i < lines.Length; i+=3)
{
string? rucksack1 = lines[i];
string? rucksack2 = lines[i + 1];
string? rucksack3 = lines[i + 2];
var sharedItems = rucksack1.Intersect(rucksack2).Intersect(rucksack3);
sum += GetPriorities(sharedItems);
}
return sum.ToString();
}
private int GetPriorities(IEnumerable<char> sharedItems) => sharedItems.Where(char.IsLetter).Sum(i => char.IsLower(i) ? i - 'a' + 1 : i - 'A' + 27);
}
}