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