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-24 21:26:39 +00:00
|
|
|
|
2023-06-07 21:56:41 +00:00
|
|
|
public Day3()
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
Console.WriteLine($@"
|
|
|
|
Day3 Solution
|
|
|
|
Part1 Result: {Part1()}
|
|
|
|
Part2 Result: {Part2()}
|
|
|
|
|
|
|
|
=============================");
|
2023-06-07 21:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int Part1()
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var sum = 0;
|
|
|
|
|
|
|
|
var duplicates = new List<char>();
|
|
|
|
|
|
|
|
foreach (var backpack in Backpacks)
|
2023-06-07 21:56:41 +00:00
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var compartmentSize = backpack.Length / 2;
|
|
|
|
var itemsInCompartment1 = new List<char>();
|
|
|
|
var itemsInCompartment2 = new List<char>();
|
|
|
|
|
|
|
|
for (var i = 0; i < compartmentSize; i++)
|
2023-06-07 21:56:41 +00:00
|
|
|
itemsInCompartment1.Add(backpack[i]);
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var i = compartmentSize; i < compartmentSize * 2; i++)
|
2023-06-07 21:56:41 +00:00
|
|
|
itemsInCompartment2.Add(backpack[i]);
|
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
var duplicatedItem = itemsInCompartment1.Intersect(itemsInCompartment2).FirstOrDefault();
|
2023-06-07 21:56:41 +00:00
|
|
|
duplicates.Add(duplicatedItem);
|
|
|
|
}
|
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
foreach (var duplicate in duplicates)
|
|
|
|
if (char.IsUpper(duplicate))
|
2023-06-07 21:56:41 +00:00
|
|
|
sum += Convert.ToInt16(duplicate) - 38;
|
|
|
|
else
|
|
|
|
sum += Convert.ToInt16(duplicate) - 96;
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
2023-06-24 21:26:39 +00:00
|
|
|
|
2023-06-07 21:56:41 +00:00
|
|
|
private static int Part2()
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var sum = 0;
|
|
|
|
var groups = new List<List<string>>();
|
2023-06-07 21:56:41 +00:00
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var i = 0; i < Backpacks.Length; i += 3)
|
2023-06-07 21:56:41 +00:00
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var group = new List<string>();
|
2023-06-07 21:56:41 +00:00
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var x = 0; x < 3; x++)
|
2023-06-07 21:56:41 +00:00
|
|
|
try
|
|
|
|
{
|
2023-06-07 23:12:01 +00:00
|
|
|
group.Add(Backpacks[i + x]);
|
2023-06-07 21:56:41 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-06-24 21:26:39 +00:00
|
|
|
|
2023-06-07 21:56:41 +00:00
|
|
|
if (group.All(x => x != ""))
|
|
|
|
groups.Add(group);
|
|
|
|
}
|
|
|
|
|
2023-06-24 21:26:39 +00:00
|
|
|
var duplicates = new List<char>();
|
|
|
|
foreach (var group in groups)
|
2023-06-07 21:56:41 +00:00
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var groupArray = group.ToArray();
|
2023-06-07 21:56:41 +00:00
|
|
|
duplicates.Add(groupArray[0].Intersect(groupArray[1].Intersect(groupArray[2])).FirstOrDefault());
|
|
|
|
}
|
2023-06-24 21:26:39 +00:00
|
|
|
|
|
|
|
foreach (var duplicate in duplicates)
|
|
|
|
if (char.IsUpper(duplicate))
|
2023-06-07 21:56:41 +00:00
|
|
|
sum += Convert.ToInt16(duplicate) - 38;
|
|
|
|
else
|
|
|
|
sum += Convert.ToInt16(duplicate) - 96;
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
}
|