AdventOfCode/Year2022/Day3.cs

89 lines
2.5 KiB
C#
Raw Normal View History

2023-06-07 21:56:41 +00:00
namespace AdventOfCode.Year2022;
public class Day3
{
2023-06-07 23:12:01 +00:00
private static readonly string[] Backpacks = File.ReadAllLines("inputs/day3.txt");
2023-06-07 21:56:41 +00:00
public Day3()
{
2023-06-07 22:59:16 +00:00
Console.WriteLine("Day3 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()
{
int sum = 0;
List<char> duplicates = new List<char>();
2023-06-07 23:12:01 +00:00
foreach (string backpack in Backpacks)
2023-06-07 21:56:41 +00:00
{
int compartmentSize = backpack.Length / 2;
List<char> itemsInCompartment1 = new List<char>();
List<char> itemsInCompartment2 = new List<char>();
for (int i = 0; i < compartmentSize; i++)
itemsInCompartment1.Add(backpack[i]);
for (int i = compartmentSize; i < compartmentSize * 2; i++)
itemsInCompartment2.Add(backpack[i]);
char duplicatedItem = itemsInCompartment1.Intersect(itemsInCompartment2).FirstOrDefault();
duplicates.Add(duplicatedItem);
}
foreach (char duplicate in duplicates)
{
if (Char.IsUpper(duplicate))
sum += Convert.ToInt16(duplicate) - 38;
else
sum += Convert.ToInt16(duplicate) - 96;
}
return sum;
}
private static int Part2()
{
int sum = 0;
List<List<string>> groups = new List<List<string>>();
2023-06-07 23:12:01 +00:00
for (int i = 0; i < Backpacks.Length; i+=3)
2023-06-07 21:56:41 +00:00
{
List<string> group = new List<string>();
for (int x = 0; x < 3; x++)
{
try
{
2023-06-07 23:12:01 +00:00
group.Add(Backpacks[i + x]);
2023-06-07 21:56:41 +00:00
}
catch
{
break;
}
}
if (group.All(x => x != ""))
groups.Add(group);
}
List<char> duplicates = new List<char>();
foreach (List<string> group in groups)
{
string[] groupArray = group.ToArray();
duplicates.Add(groupArray[0].Intersect(groupArray[1].Intersect(groupArray[2])).FirstOrDefault());
}
foreach (char duplicate in duplicates)
{
if (Char.IsUpper(duplicate))
sum += Convert.ToInt16(duplicate) - 38;
else
sum += Convert.ToInt16(duplicate) - 96;
}
return sum;
}
}