Moved 2023 to new core lib

This commit is contained in:
Rob 2023-12-04 20:29:20 +01:00
parent f9fe8f48f2
commit e59dd074fd
7 changed files with 11 additions and 202 deletions

View File

@ -6,7 +6,10 @@ InputReader inputReader = new()
{
//IsDebug = true
};
IChallange challange = new Day04(inputReader);
inputReader.SetInputByChallange(2);
IChallange challange = new Day02(inputReader);
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");

View File

@ -42,7 +42,7 @@ namespace AdventOfCode.Core
}
}
public Task<string[]> ReadAsArraytString()
public Task<string[]> ReadAsArrayString()
{
return File.ReadAllLinesAsync(InputFilePath);
}

View File

@ -1,31 +0,0 @@
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day01Part1 : Answerable
{
public override int Year { get; set; } = 2023;
public override int Day { get; set; } = 1;
public override int Part { get; set; } = 1;
public override string GetAnswer(byte[] data)
{
return GetAsStringArray(data)
.Select(GetValueFromLine)
.Sum()
.ToString();
}
private int GetValueFromLine(string line)
{
// get all the numers
MatchCollection collection = Regex.Matches(line, @"\d");
// merge first and last index as one
string values = collection[0].Value + collection[^1].Value;
// make an int
return int.Parse(values);
}
}
}

View File

@ -1,79 +0,0 @@
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day01Part2 : Answerable
{
public override int Year { get; set; } = 2023;
public override int Day { get; set; } = 1;
public override int Part { get; set; } = 2;
public override string GetAnswer(byte[] data)
{
return GetAsStringArray(data)
.Select(GetValueFromLine)
.Sum()
.ToString();
}
private int GetValueFromLine(string line)
{
MatchCollection matchCollection = Regex.Matches(line, @"(\d)");
List<Match> captures = matchCollection.ToList();
// replace the text with numbers
captures.AddRange(Regex.Matches(line, @"(one)"));
captures.AddRange(Regex.Matches(line, @"(two)"));
captures.AddRange(Regex.Matches(line, @"(three)"));
captures.AddRange(Regex.Matches(line, @"(four)"));
captures.AddRange(Regex.Matches(line, @"(five)"));
captures.AddRange(Regex.Matches(line, @"(six)"));
captures.AddRange(Regex.Matches(line, @"(seven)"));
captures.AddRange(Regex.Matches(line, @"(eight)"));
captures.AddRange(Regex.Matches(line, @"(nine)"));
captures = captures.OrderBy(x => x.Index).ToList();
// merge first and last index as one
string values = GetStringAsNumber(captures.First().Value) + GetStringAsNumber(captures.Last().Value);
// make an int
return int.Parse(values);
}
private string GetStringAsNumber(string value)
{
switch(value)
{
case "one":
case "1":
return "1";
case "two":
case "2":
return "2";
case "three":
case "3":
return "3";
case "four":
case "4":
return "4";
case "five":
case "5":
return "5";
case "six":
case "6":
return "6";
case "seven":
case "7":
return "7";
case "eight":
case "8":
return "8";
case "nine":
case "9":
return "9";
default:
return string.Empty;
}
}
}
}

View File

@ -1,49 +0,0 @@
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day02Part1 : Answerable
{
public override int Year { get; set; } = 2023;
public override int Day { get; set; } = 2;
public override int Part { get; set; } = 1;
public override string GetAnswer(byte[] data)
{
return GetAsStringArray(data)
.Select(ParseGames)
.Sum()
.ToString();
}
private int ParseGames(string line)
{
// get all the numers
string gameId = line.Split(':')[0].Split(' ')[1];
string[] dicePulls = line.Split(':')[1].Split(';');
// parse the pulls
foreach (string dicePull in dicePulls)
{
// regex the pull
MatchCollection dice = Regex.Matches(dicePull, "(\\d+) (g|r|b)");
foreach (Match match in dice)
{
string[] splitted = match.Value.Split(' ');
int ammount = int.Parse(splitted[0]);
if (ammount <= 12) // pass for all dice
continue;
if (ammount > 14 // fail for any dice set
|| (splitted[1] == "r" && ammount > 12)
|| (splitted[1] == "g" && ammount > 13))
return 0; // not possible
}
}
return int.Parse(gameId);
}
}
}

View File

@ -1,41 +0,0 @@
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day02Part2 : Answerable
{
public override int Year { get; set; } = 2023;
public override int Day { get; set; } = 2;
public override int Part { get; set; } = 2;
public override string GetAnswer(byte[] data)
{
return GetAsStringArray(data)
.Select(GetValueFromLine)
.Sum()
.ToString();
}
private int GetValueFromLine(string line)
{
// get all the numers
string dicePulls = line.Split(':')[1];
// parse the pulls
MatchCollection dice = Regex.Matches(dicePulls, "(\\d+) (g|r|b)");
int red = 0, green = 0, blue = 0;
foreach (Match match in dice)
{
string[] splitted = match.Value.Split(' ');
int ammount = int.Parse(splitted[0]);
if (splitted[1] == "r" && ammount > red) { red = ammount; }
else if (splitted[1] == "g" && ammount > green) { green = ammount; }
else if (splitted[1] == "b" && ammount > blue) { blue = ammount; }
}
return red * green * blue;
}
}
}

View File

@ -12,6 +12,12 @@
</ItemGroup>
<ItemGroup>
<Compile Update="2023\Day 01\Day01.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 02\Day02.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="2023\Day 04\Day04.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>