From 8b27e0d72a2b5fe7976decf9c5b4a6f81d7d9a8d Mon Sep 17 00:00:00 2001 From: cyberdick Date: Sat, 3 Dec 2022 19:27:34 +0530 Subject: [PATCH] moved splitting compartments into puzzle 1 as its not required in puzzle 2 --- AOC2022/Puzzles/Day3.cs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/AOC2022/Puzzles/Day3.cs b/AOC2022/Puzzles/Day3.cs index 6a25a43..a955701 100644 --- a/AOC2022/Puzzles/Day3.cs +++ b/AOC2022/Puzzles/Day3.cs @@ -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)