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