moved splitting compartments into puzzle 1 as its not required in puzzle 2

master
cyberdick 2022-12-03 19:27:34 +05:30
parent bd14f76482
commit 8b27e0d72a
1 changed files with 9 additions and 17 deletions

View File

@ -4,41 +4,33 @@ internal class Day3 : IPuzzle
{
private readonly string[] _linesFromFile;
private readonly int noOfRuckSacks;
private string[] _leftComparments;
private string[] _rightCompartments;
public Day3(string[] linesFromFile)
{
_linesFromFile = linesFromFile;
noOfRuckSacks = linesFromFile.Length;
SplitRuckSacks();
}
private void SplitRuckSacks()
public void FirstPuzzle()
{
_leftComparments = new string[noOfRuckSacks];
_rightCompartments = new string[noOfRuckSacks];
var leftComparments = new string[noOfRuckSacks];
var rightCompartments = new string[noOfRuckSacks];
var index = 0;
foreach (var line in _linesFromFile)
{
var ruckSack = line.Trim();
var left = ruckSack[..(ruckSack.Length / 2)];
var right = ruckSack[(ruckSack.Length / 2)..];
_leftComparments[index] = left;
_rightCompartments[index] = right;
leftComparments[index] = left;
rightCompartments[index] = right;
index++;
}
}
public void FirstPuzzle()
{
var prioritySum = 0;
for (int i = 0; i < noOfRuckSacks; i++)
{
char duplicateItem = ' ';
foreach (var item in _leftComparments[i])
foreach (var item in leftComparments[i])
{
if (_rightCompartments[i].Contains(item))
if (rightCompartments[i].Contains(item))
{
duplicateItem = item;
break;
@ -46,7 +38,7 @@ internal class Day3 : IPuzzle
}
prioritySum += EvaluatePriority(duplicateItem);
}
Console.WriteLine(prioritySum.ToString());
Console.WriteLine($"Priority sum of duplicate items in compartments: {prioritySum}");
}
public void SecondPuzzle()
@ -68,7 +60,7 @@ internal class Day3 : IPuzzle
}
prioritySum += EvaluatePriority(authenticItem);
}
Console.WriteLine(prioritySum.ToString());
Console.WriteLine($"Priority sum of authentic items groups of 3 elves: {prioritySum}");
}
private static int EvaluatePriority(char ruckSackItem)