complete day3 part 1
parent
8c91d6e206
commit
ac197c4d2b
|
@ -2,7 +2,7 @@
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"WithParams": {
|
"WithParams": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"commandLineArgs": "1 2",
|
"commandLineArgs": "3",
|
||||||
"workingDirectory": "./"
|
"workingDirectory": "./"
|
||||||
},
|
},
|
||||||
"WithoutParams": {
|
"WithoutParams": {
|
||||||
|
|
|
@ -2,13 +2,71 @@ namespace AOC2022.Puzzles;
|
||||||
|
|
||||||
internal class Day3 : IPuzzle
|
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()
|
||||||
|
{
|
||||||
|
_leftComparments = new string[noOfRuckSacks];
|
||||||
|
_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;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void FirstPuzzle()
|
public void FirstPuzzle()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var prioritySum = 0;
|
||||||
|
for (int i = 0; i < noOfRuckSacks; i++)
|
||||||
|
{
|
||||||
|
char duplicateItem = ' ';
|
||||||
|
foreach (var item in _leftComparments[i])
|
||||||
|
{
|
||||||
|
if (_rightCompartments[i].Contains(item))
|
||||||
|
{
|
||||||
|
duplicateItem = item;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prioritySum += EvaluatePriority(duplicateItem);
|
||||||
|
}
|
||||||
|
Console.WriteLine(prioritySum.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SecondPuzzle()
|
public void SecondPuzzle()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int EvaluatePriority(char ruckSackItem)
|
||||||
|
{
|
||||||
|
if (char.IsUpper(ruckSackItem))
|
||||||
|
{
|
||||||
|
return (ruckSackItem - 'A') + 27;
|
||||||
|
}
|
||||||
|
else if (char.IsLower(ruckSackItem))
|
||||||
|
{
|
||||||
|
return (ruckSackItem - 'a') + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue