AdventOfCode 2023 Day01

This commit is contained in:
2023-12-01 18:23:53 +01:00
parent 52c9dab8a6
commit 4d8bfbb377
10 changed files with 1291 additions and 0 deletions

View File

@@ -15,6 +15,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2018", "AdventO
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2018.Tests", "AdventOfCode2018.Tests\AdventOfCode2018.Tests.csproj", "{EE35694D-97C1-485D-9FFE-73912FA97446}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2023", "AdventOfCode2023\AdventOfCode2023.csproj", "{0D90084C-D2D9-489E-BFC3-5E63AE9947B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode2023.Tests", "AdventOfCode2023.Tests\AdventOfCode2023.Tests.csproj", "{00869F26-F53E-451D-BEA2-4E3FFE2216FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +49,14 @@ Global
{EE35694D-97C1-485D-9FFE-73912FA97446}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE35694D-97C1-485D-9FFE-73912FA97446}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE35694D-97C1-485D-9FFE-73912FA97446}.Release|Any CPU.Build.0 = Release|Any CPU
{0D90084C-D2D9-489E-BFC3-5E63AE9947B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D90084C-D2D9-489E-BFC3-5E63AE9947B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D90084C-D2D9-489E-BFC3-5E63AE9947B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D90084C-D2D9-489E-BFC3-5E63AE9947B3}.Release|Any CPU.Build.0 = Release|Any CPU
{00869F26-F53E-451D-BEA2-4E3FFE2216FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00869F26-F53E-451D-BEA2-4E3FFE2216FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00869F26-F53E-451D-BEA2-4E3FFE2216FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00869F26-F53E-451D-BEA2-4E3FFE2216FB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,28 @@
<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="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdventOfCode2023\AdventOfCode2023.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,37 @@
namespace AdventOfCode2023.Tests;
public class Day01_Tests
{
[Fact]
public void ResolvePart1__Example()
{
var day = new Day01();
string result = day.ResolvePart1(new string[] {
"1abc2",
"pqr3stu8vwx",
"a1b2c3d4e5f",
"treb7uchet",
});
Assert.Equal("142", result);
}
[Fact]
public void ResolvePart2__Example()
{
var day = new Day01();
string result = day.ResolvePart2(new string[] {
"two1nine",
"eightwothree",
"abcone2threexyz",
"xtwone3four",
"4nineeightseven2",
"zoneight234",
"7pqrstsixteen",
});
Assert.Equal("281", result);
}
}

View File

@@ -0,0 +1 @@
global using Xunit;

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>AdventOfCode2023</RootNamespace>
</PropertyGroup>
</Project>

142
AdventOfCode2023/Day01.cs Normal file
View File

@@ -0,0 +1,142 @@
namespace AdventOfCode2023;
/*
--- Day 1: Trebuchet?! ---
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
You've been doing this long enough to know that to restore snow operations, you need to check all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
For example:
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
In this example, the calibration values of these four lines are 12, 38, 15, and 77. Adding these together produces 142.
Consider your entire calibration document. What is the sum of all of the calibration values?
--- Part Two ---
Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.
What is the sum of all of the calibration values?
*/
public class Day01 : IDay
{
public string ResolvePart1(string[] inputs)
{
int sum = 0;
foreach (string line in inputs)
{
int firstDigit = 0;
int lastDigit = 0;
foreach (char c in line)
{
if (char.IsDigit(c) == false)
{
continue;
}
lastDigit = c - '0';
if (firstDigit == 0)
{
firstDigit = lastDigit;
}
}
int value = firstDigit * 10 + lastDigit;
sum += value;
}
return sum.ToString();
}
public string ResolvePart2(string[] inputs)
{
List<(string Text, int Value)> digits = new List<(string Text, int Value)>
{
("1", 1),
("2", 2),
("3", 3),
("4", 4),
("5", 5),
("6", 6),
("7", 7),
("8", 8),
("9", 9),
("one", 1),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
};
int sum = 0;
foreach (string line in inputs)
{
int firstDigit = 0;
int lastDigit = 0;
for (int i = 0; i < line.Length; i++)
{
for (int j = 0; j < digits.Count; j++)
{
bool matched = true;
for (int k = 0; k < digits[j].Text.Length; k++)
{
int offset = i + k;
if (line.Length == offset || line[offset] != digits[j].Text[k])
{
matched = false;
break;
}
}
if (matched == false)
{
continue;
}
lastDigit = digits[j].Value;
if (firstDigit == 0)
{
firstDigit = lastDigit;
}
}
}
int value = firstDigit * 10 + lastDigit;
sum += value;
}
return sum.ToString();
}
}

53
AdventOfCode2023/IDay.cs Normal file
View File

@@ -0,0 +1,53 @@
namespace AdventOfCode2023;
public interface IDay
{
string ResolvePart1(string[] inputs);
string ResolvePart2(string[] inputs);
}
public static class DayHelper
{
public static void RunDay(int currentDayNumber)
{
Console.WriteLine($"Day {currentDayNumber:00}");
Console.WriteLine("------");
Console.WriteLine();
IDay? currentDay = null;
Type? dayType = Type.GetType($"AdventOfCode2023.Day{currentDayNumber:00}");
if (dayType != null)
{
currentDay = Activator.CreateInstance(dayType) as IDay;
}
if (currentDay == null)
{
Console.WriteLine("!!!!!!!");
Console.WriteLine("Day implementation not found.");
return;
}
string[] linesDay = File.ReadAllLines($"inputs/Day{currentDayNumber:00}.txt");
try
{
string resultPart1 = currentDay.ResolvePart1(linesDay);
Console.WriteLine("Day{1:00} Result Part1: {0}", resultPart1, currentDayNumber);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
try
{
string resultPart2 = currentDay.ResolvePart2(linesDay);
Console.WriteLine("Day{1:00} Result Part2: {0}", resultPart2, currentDayNumber);
}
catch (Exception ex)
{
Console.WriteLine("!!!!!!!");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}

View File

@@ -0,0 +1,6 @@
int currentDayNumber = 1;
DayHelper.RunDay(currentDayNumber);
Console.Read();

View File

@@ -0,0 +1 @@
global using AdventOfCode2023;

File diff suppressed because it is too large Load Diff