minor refactors and optimizations
parent
dad144db00
commit
9d041d261d
|
@ -4,7 +4,7 @@ namespace AOC2022;
|
|||
|
||||
internal static class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Enter Days separated by space, like '1 2 3 4 5'");
|
||||
try
|
||||
|
@ -16,26 +16,30 @@ internal static class Program
|
|||
foreach (var file in Directory.GetFiles("Inputs/"))
|
||||
{
|
||||
var fileName = Path.GetFileNameWithoutExtension(file);
|
||||
if (!int.TryParse(fileName, out int dayNumber))
|
||||
if (!int.TryParse(fileName, out var dayNumber))
|
||||
{
|
||||
Console.WriteLine("Just name the files 1.txt or something ffs");
|
||||
return;
|
||||
}
|
||||
|
||||
daysToProcess.Add(dayNumber);
|
||||
}
|
||||
|
||||
ProcessDays(daysToProcess);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var arg in args)
|
||||
{
|
||||
if (!int.TryParse(arg, out int dayNumber))
|
||||
if (!int.TryParse(arg, out var dayNumber))
|
||||
{
|
||||
Console.WriteLine("Just enter the numbers dude");
|
||||
return;
|
||||
}
|
||||
|
||||
daysToProcess.Add(dayNumber);
|
||||
}
|
||||
|
||||
ProcessDays(daysToProcess);
|
||||
}
|
||||
}
|
||||
|
@ -45,38 +49,34 @@ internal static class Program
|
|||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ProcessDays(List<int> dayNumbers)
|
||||
{
|
||||
try
|
||||
{
|
||||
dayNumbers.ForEach(day =>
|
||||
{
|
||||
string instanceToCreate = "AOC2022.Puzzles.Day" + day;
|
||||
Type? typeName = Type.GetType(typeName: instanceToCreate);
|
||||
var instanceToCreate = "AOC2022.Puzzles.Day" + day;
|
||||
var typeName = Type.GetType(typeName: instanceToCreate);
|
||||
|
||||
var linesInFile = File.ReadAllLines($"Inputs/{day}.txt");
|
||||
if (typeName != null)
|
||||
{
|
||||
if (typeName == null) return;
|
||||
|
||||
if (Activator.CreateInstance(typeName, new object[] {linesInFile}) is IPuzzle puzzle)
|
||||
{
|
||||
Console.WriteLine($"Day {day} answers");
|
||||
puzzle.FirstPuzzle();
|
||||
puzzle.SecondPuzzle();
|
||||
puzzle.SolveFirstPuzzle();
|
||||
puzzle.SolveSecondPuzzle();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Something Broke");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (NotImplementedException nex)
|
||||
{
|
||||
Console.WriteLine(nex.Message);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Console.WriteLine($"You broke it {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,16 +8,10 @@ internal class Day1 : IPuzzle
|
|||
public Day1(string[] linesFromFile)
|
||||
{
|
||||
_linesFromFile = linesFromFile;
|
||||
try
|
||||
{
|
||||
GetElfFoodSumList();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
void GetElfFoodSumList()
|
||||
|
||||
private void GetElfFoodSumList()
|
||||
{
|
||||
var elfFoodSum = 0;
|
||||
//var elfFoodSumList = new List<int>();
|
||||
|
@ -32,15 +26,17 @@ internal class Day1 : IPuzzle
|
|||
_elfFoodSumList.Add(elfFoodSum);
|
||||
elfFoodSum = 0;
|
||||
}
|
||||
|
||||
_elfFoodSumList.Sort((x, y) => y.CompareTo(x));
|
||||
}
|
||||
}
|
||||
public void FirstPuzzle()
|
||||
|
||||
public void SolveFirstPuzzle()
|
||||
{
|
||||
Console.WriteLine($"Fattest fucker has {_elfFoodSumList[0]} borgers");
|
||||
}
|
||||
|
||||
public void SecondPuzzle()
|
||||
public void SolveSecondPuzzle()
|
||||
{
|
||||
Console.WriteLine($"Top 3 Fatties have {_elfFoodSumList.Take(3).Sum()} borgers");
|
||||
}
|
||||
|
|
|
@ -8,24 +8,17 @@ internal class Day2 : IPuzzle
|
|||
{
|
||||
_linesFromFile = linesFromFile;
|
||||
}
|
||||
public void FirstPuzzle()
|
||||
{
|
||||
int score = 0;
|
||||
|
||||
foreach (var line in _linesFromFile)
|
||||
public void SolveFirstPuzzle()
|
||||
{
|
||||
score += ((line[2] - line[0] - 1) % 3 * 3) + (line[2] - 'X' + 1);
|
||||
}
|
||||
var score = _linesFromFile.Sum(line => (line[2] - line[0] - 1) % 3 * 3 + (line[2] - 'X') + 1);
|
||||
|
||||
Console.WriteLine($"Elf trickery knows no bounds, without the elf the score is {score}");
|
||||
}
|
||||
|
||||
public void SecondPuzzle()
|
||||
public void SolveSecondPuzzle()
|
||||
{
|
||||
int score = 0;
|
||||
foreach (var line in _linesFromFile)
|
||||
{
|
||||
score += ((line[0] - 'A' + line[2] - 'X' + 2) % 3) + 1 + (line[2] - 'X') * 3;
|
||||
}
|
||||
var score = _linesFromFile.Sum(line => (line[0] - 'A' + line[2] - 'X' + 2) % 3 + 1 + (line[2] - 'X') * 3);
|
||||
|
||||
Console.WriteLine($"Elf trickery knows no bounds, with the elf help the score is {score}");
|
||||
}
|
||||
|
|
|
@ -3,63 +3,63 @@ namespace AOC2022.Puzzles;
|
|||
internal class Day3 : IPuzzle
|
||||
{
|
||||
private readonly string[] _linesFromFile;
|
||||
private readonly int noOfRuckSacks;
|
||||
private readonly int _noOfRuckSacks;
|
||||
|
||||
public Day3(string[] linesFromFile)
|
||||
{
|
||||
_linesFromFile = linesFromFile;
|
||||
noOfRuckSacks = linesFromFile.Length;
|
||||
_noOfRuckSacks = linesFromFile.Length;
|
||||
}
|
||||
public void FirstPuzzle()
|
||||
|
||||
public void SolveFirstPuzzle()
|
||||
{
|
||||
var leftComparments = new string[noOfRuckSacks];
|
||||
var rightCompartments = new string[noOfRuckSacks];
|
||||
var leftCompartments = 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;
|
||||
|
||||
leftCompartments[index] = ruckSack[..(ruckSack.Length / 2)];
|
||||
rightCompartments[index] = ruckSack[(ruckSack.Length / 2)..];
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
var prioritySum = 0;
|
||||
for (int i = 0; i < noOfRuckSacks; i++)
|
||||
for (var i = 0; i < _noOfRuckSacks; i++)
|
||||
{
|
||||
char duplicateItem = ' ';
|
||||
foreach (var item in leftComparments[i])
|
||||
{
|
||||
if (rightCompartments[i].Contains(item))
|
||||
var duplicateItem = ' ';
|
||||
foreach (var item in leftCompartments[i].Where(item => rightCompartments[i].Contains(item)))
|
||||
{
|
||||
duplicateItem = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
prioritySum += EvaluatePriority(duplicateItem);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Priority sum of duplicate items in compartments: {prioritySum}");
|
||||
}
|
||||
|
||||
public void SecondPuzzle()
|
||||
public void SolveSecondPuzzle()
|
||||
{
|
||||
var prioritySum = 0;
|
||||
for (int i = 0; i < noOfRuckSacks; i += 3)
|
||||
for (var i = 0; i < _noOfRuckSacks; i += 3)
|
||||
{
|
||||
var elf1Sack = _linesFromFile[i].Trim();
|
||||
var elf2Sack = _linesFromFile[i + 1].Trim();
|
||||
var elf3Sack = _linesFromFile[i + 2].Trim();
|
||||
char authenticItem = ' ';
|
||||
foreach (var item in elf1Sack)
|
||||
{
|
||||
if (elf2Sack.Contains(item) && elf3Sack.Contains(item))
|
||||
var authenticItem = ' ';
|
||||
foreach (var item in elf1Sack.Where(item => elf2Sack.Contains(item) && elf3Sack.Contains(item)))
|
||||
{
|
||||
authenticItem = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
prioritySum += EvaluatePriority(authenticItem);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Priority sum of authentic items groups of 3 elves: {prioritySum}");
|
||||
}
|
||||
|
||||
|
@ -67,15 +67,14 @@ internal class Day3 : IPuzzle
|
|||
{
|
||||
if (char.IsUpper(ruckSackItem))
|
||||
{
|
||||
return (ruckSackItem - 'A') + 27;
|
||||
return ruckSackItem - 'A' + 27;
|
||||
}
|
||||
else if (char.IsLower(ruckSackItem))
|
||||
|
||||
if (char.IsLower(ruckSackItem))
|
||||
{
|
||||
return (ruckSackItem - 'a') + 1;
|
||||
return ruckSackItem - 'a' + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
namespace AOC2022.Puzzles
|
||||
{
|
||||
namespace AOC2022.Puzzles;
|
||||
|
||||
internal interface IPuzzle
|
||||
{
|
||||
void FirstPuzzle();
|
||||
void SecondPuzzle();
|
||||
}
|
||||
void SolveFirstPuzzle();
|
||||
void SolveSecondPuzzle();
|
||||
}
|
Loading…
Reference in New Issue