Implemented Base and finished Day1 Part 1 and Part 2
This commit is contained in:
parent
bd87d2359a
commit
f53db06c66
@ -5,6 +5,8 @@ VisualStudioVersion = 17.4.33110.190
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2023", "AdventOfCode2023\AdventOfCode2023.csproj", "{415F4228-5CB8-4CD6-9707-52042645DF48}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2023", "AdventOfCode2023\AdventOfCode2023.csproj", "{415F4228-5CB8-4CD6-9707-52042645DF48}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2023Tests", "AdventOfCode2023Tests\AdventOfCode2023Tests.csproj", "{F6BDFA63-5763-4D29-865F-A997A5855C5D}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -15,6 +17,10 @@ Global
|
|||||||
{415F4228-5CB8-4CD6-9707-52042645DF48}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{415F4228-5CB8-4CD6-9707-52042645DF48}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{415F4228-5CB8-4CD6-9707-52042645DF48}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{415F4228-5CB8-4CD6-9707-52042645DF48}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{415F4228-5CB8-4CD6-9707-52042645DF48}.Release|Any CPU.Build.0 = Release|Any CPU
|
{415F4228-5CB8-4CD6-9707-52042645DF48}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F6BDFA63-5763-4D29-865F-A997A5855C5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F6BDFA63-5763-4D29-865F-A997A5855C5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F6BDFA63-5763-4D29-865F-A997A5855C5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F6BDFA63-5763-4D29-865F-A997A5855C5D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -7,4 +7,12 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Inputs\Day1.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Inputs\Day1.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
51
AdventOfCode2023/Day1/Day1_1.cs
Normal file
51
AdventOfCode2023/Day1/Day1_1.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AdventOfCode2023.Day1
|
||||||
|
{
|
||||||
|
public class Day1_1 : DayBase
|
||||||
|
{
|
||||||
|
string input = string.Empty;
|
||||||
|
Regex singleDigit = new Regex(@"\d");
|
||||||
|
|
||||||
|
public Day1_1()
|
||||||
|
{
|
||||||
|
input = GetInput("Day1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Day1_1(string testInput)
|
||||||
|
{
|
||||||
|
input = testInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Execute()
|
||||||
|
{
|
||||||
|
var segmentedInput = input.Split("\r\n");
|
||||||
|
int calibrationSum = 0;
|
||||||
|
|
||||||
|
foreach (var segment in segmentedInput)
|
||||||
|
{
|
||||||
|
if (!singleDigit.IsMatch(segment))
|
||||||
|
{
|
||||||
|
return $"segment: {segment} does not contain a digit!";
|
||||||
|
}
|
||||||
|
|
||||||
|
var matches = Regex.Matches(segment, @"\d");
|
||||||
|
if (int.TryParse(string.Join("", matches.FirstOrDefault()?.Value ?? "", matches.LastOrDefault()?.Value ?? ""), out var calibrationValue))
|
||||||
|
{
|
||||||
|
calibrationSum += calibrationValue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $"Could not parse segment: {segment} to int!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return calibrationSum.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
72
AdventOfCode2023/Day1/Day1_2.cs
Normal file
72
AdventOfCode2023/Day1/Day1_2.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AdventOfCode2023.Day1
|
||||||
|
{
|
||||||
|
public class Day1_2 : DayBase
|
||||||
|
{
|
||||||
|
string input = string.Empty;
|
||||||
|
Regex leftSingleDigit = new Regex(@"\d|one|two|three|four|five|six|seven|eight|nine");
|
||||||
|
Regex rightSingleDigit = new Regex(@"\d|one|two|three|four|five|six|seven|eight|nine", RegexOptions.RightToLeft);
|
||||||
|
Dictionary<string, string> map = new Dictionary<string, string>() {
|
||||||
|
{ "one", "1" },
|
||||||
|
{ "two", "2" },
|
||||||
|
{ "three", "3" },
|
||||||
|
{ "four", "4" },
|
||||||
|
{ "five", "5" },
|
||||||
|
{ "six", "6" },
|
||||||
|
{ "seven", "7" },
|
||||||
|
{ "eight", "8" },
|
||||||
|
{ "nine", "9" },
|
||||||
|
};
|
||||||
|
|
||||||
|
public Day1_2()
|
||||||
|
{
|
||||||
|
input = GetInput("Day1.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
public Day1_2(string testInput)
|
||||||
|
{
|
||||||
|
input = testInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Execute()
|
||||||
|
{
|
||||||
|
var segmentedInput = input.Split("\r\n");
|
||||||
|
int calibrationSum = 0;
|
||||||
|
|
||||||
|
foreach (var segment in segmentedInput)
|
||||||
|
{
|
||||||
|
if (!leftSingleDigit.IsMatch(segment))
|
||||||
|
{
|
||||||
|
return $"segment: {segment} does not contain a digit!";
|
||||||
|
}
|
||||||
|
|
||||||
|
var leftMatch = leftSingleDigit.Match(segment);
|
||||||
|
var rightMatch = rightSingleDigit.Match(segment);
|
||||||
|
|
||||||
|
var joinedString = string.Join("", leftMatch.Value ?? "", rightMatch.Value ?? "");
|
||||||
|
var digitizedString = joinedString;
|
||||||
|
foreach (var item in map)
|
||||||
|
{
|
||||||
|
digitizedString = digitizedString.Replace(item.Key, item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (int.TryParse(digitizedString, out var calibrationValue))
|
||||||
|
{
|
||||||
|
calibrationSum += calibrationValue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $"Could not parse segment: {digitizedString} to int!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return calibrationSum.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
55
AdventOfCode2023/DayBase.cs
Normal file
55
AdventOfCode2023/DayBase.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AdventOfCode2023
|
||||||
|
{
|
||||||
|
public abstract class DayBase
|
||||||
|
{
|
||||||
|
public enum Encodings
|
||||||
|
{
|
||||||
|
Default,
|
||||||
|
UTF8,
|
||||||
|
ASCII
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetInput(string fileName, Encodings encoding = Encodings.Default)
|
||||||
|
{
|
||||||
|
Encoding enc;
|
||||||
|
|
||||||
|
switch (encoding)
|
||||||
|
{
|
||||||
|
case Encodings.Default:
|
||||||
|
enc = Encoding.Default;
|
||||||
|
break;
|
||||||
|
case Encodings.UTF8:
|
||||||
|
enc = Encoding.UTF8;
|
||||||
|
break;
|
||||||
|
case Encodings.ASCII:
|
||||||
|
enc = Encoding.ASCII;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
enc = Encoding.Default;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var assembly = Assembly.GetExecutingAssembly();
|
||||||
|
var resourceName = $"AdventOfCode2023.Inputs.{fileName}";
|
||||||
|
|
||||||
|
string result = string.Empty;
|
||||||
|
|
||||||
|
using (Stream stream = assembly.GetManifestResourceStream(resourceName) ?? throw new FileNotFoundException($"Could not find resource: {resourceName}!"))
|
||||||
|
using (StreamReader reader = new StreamReader(stream, enc))
|
||||||
|
{
|
||||||
|
result = reader.ReadToEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract string Execute();
|
||||||
|
}
|
||||||
|
}
|
1000
AdventOfCode2023/Inputs/Day1.txt
Normal file
1000
AdventOfCode2023/Inputs/Day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,28 @@
|
|||||||
namespace AdventOfCode2023
|
using AdventOfCode2023.Day1;
|
||||||
|
|
||||||
|
namespace AdventOfCode2023
|
||||||
{
|
{
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
|
private static List<DayBase> days = new List<DayBase>() {
|
||||||
|
new Day1_1(),
|
||||||
|
new Day1_2(),
|
||||||
|
};
|
||||||
|
|
||||||
|
private static void ListDays()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Select Day:");
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
days.ForEach(item => Console.WriteLine($"{index++} - {item.GetType().Name}"));
|
||||||
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Hello, World!");
|
ListDays();
|
||||||
|
Console.WriteLine(days[int.Parse(Console.ReadLine() ?? "")].Execute());
|
||||||
|
Console.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
23
AdventOfCode2023Tests/AdventOfCode2023Tests.csproj
Normal file
23
AdventOfCode2023Tests/AdventOfCode2023Tests.csproj
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||||
|
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||||
|
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||||
|
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.1.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AdventOfCode2023\AdventOfCode2023.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
26
AdventOfCode2023Tests/UnitTests.cs
Normal file
26
AdventOfCode2023Tests/UnitTests.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
namespace AdventOfCode2023Tests
|
||||||
|
{
|
||||||
|
public class Tests
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Day1_1Test()
|
||||||
|
{
|
||||||
|
var day1_1 = new Day1_1("1abc2\r\npqr3stu8vwx\r\na1b2c3d4e5f\r\ntreb7uchet");
|
||||||
|
Assert.That(day1_1.Execute(), Is.EqualTo("142"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Day1_2Test()
|
||||||
|
{
|
||||||
|
var day1_2 = new Day1_2("two1nine\r\neightwothree\r\nabcone2threexyz\r\nxtwone3four\r\n4nineeightseven2\r\nzoneight234\r\n7pqrstsixteen");
|
||||||
|
Assert.That(day1_2.Execute(), Is.EqualTo("281"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Day1_2CustomTest()
|
||||||
|
{
|
||||||
|
var day1_2 = new Day1_2("1oneight");
|
||||||
|
Assert.That(day1_2.Execute(), Is.EqualTo("18"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
AdventOfCode2023Tests/Usings.cs
Normal file
3
AdventOfCode2023Tests/Usings.cs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
global using NUnit.Framework;
|
||||||
|
global using AdventOfCode2023;
|
||||||
|
global using AdventOfCode2023.Day1;
|
Loading…
Reference in New Issue
Block a user