Skip to content

Commit

Permalink
Add day 01 puzzles solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianLfdr committed Dec 2, 2023
1 parent 3f3f30c commit f4920bd
Show file tree
Hide file tree
Showing 11 changed files with 1,165 additions and 197 deletions.
34 changes: 0 additions & 34 deletions src/AdventOfCode.Usage/AdventOfCode.Usage.csproj

This file was deleted.

26 changes: 0 additions & 26 deletions src/AdventOfCode.Usage/WithLocalInput/Solver.cs

This file was deleted.

32 changes: 0 additions & 32 deletions src/AdventOfCode.Usage/WithLocalInput/SolverTest.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/AdventOfCode.Usage/WithLocalInput/input.txt

This file was deleted.

32 changes: 0 additions & 32 deletions src/AdventOfCode.Usage/WithRemoteInput/Solver.cs

This file was deleted.

32 changes: 0 additions & 32 deletions src/AdventOfCode.Usage/WithRemoteInput/SolverTest.cs

This file was deleted.

77 changes: 41 additions & 36 deletions src/advent-of-code.sln → src/AdventOfCode.sln
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Commons", "AdventOfCode.Commons\AdventOfCode.Commons.csproj", "{79A796F8-0516-48B6-91DD-00EE788954F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Usage", "AdventOfCode.Usage\AdventOfCode.Usage.csproj", "{2CE56D7F-C728-47AB-ADE1-2ABC17EABFAF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9D5A0EF-004F-4163-8F74-31F89FA01DD0}"
ProjectSection(SolutionItems) = preProject
..\.github\workflows\dotnet.yml = ..\.github\workflows\dotnet.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{79A796F8-0516-48B6-91DD-00EE788954F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79A796F8-0516-48B6-91DD-00EE788954F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79A796F8-0516-48B6-91DD-00EE788954F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79A796F8-0516-48B6-91DD-00EE788954F8}.Release|Any CPU.Build.0 = Release|Any CPU
{2CE56D7F-C728-47AB-ADE1-2ABC17EABFAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CE56D7F-C728-47AB-ADE1-2ABC17EABFAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CE56D7F-C728-47AB-ADE1-2ABC17EABFAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CE56D7F-C728-47AB-ADE1-2ABC17EABFAF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D5F58221-D5D2-4C5D-831E-1CB095469000}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode.Commons", "AdventOfCode.Commons\AdventOfCode.Commons.csproj", "{79A796F8-0516-48B6-91DD-00EE788954F8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9D5A0EF-004F-4163-8F74-31F89FA01DD0}"
ProjectSection(SolutionItems) = preProject
..\.github\workflows\dotnet.yml = ..\.github\workflows\dotnet.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solutions", "Solutions", "{D4A0739B-FCDE-4893-BD26-29C3EC4E661A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day01", "Solutions\Day01\Day01.csproj", "{F093775E-73AB-41B9-9375-96F70E4D9731}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{79A796F8-0516-48B6-91DD-00EE788954F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79A796F8-0516-48B6-91DD-00EE788954F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79A796F8-0516-48B6-91DD-00EE788954F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79A796F8-0516-48B6-91DD-00EE788954F8}.Release|Any CPU.Build.0 = Release|Any CPU
{F093775E-73AB-41B9-9375-96F70E4D9731}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F093775E-73AB-41B9-9375-96F70E4D9731}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F093775E-73AB-41B9-9375-96F70E4D9731}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F093775E-73AB-41B9-9375-96F70E4D9731}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F093775E-73AB-41B9-9375-96F70E4D9731} = {D4A0739B-FCDE-4893-BD26-29C3EC4E661A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D5F58221-D5D2-4C5D-831E-1CB095469000}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions src/Solutions/Day01/Day01.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\AdventOfCode.Commons\AdventOfCode.Commons.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="input.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions src/Solutions/Day01/Solver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using AdventOfCode.Commons;
using System.Text.RegularExpressions;

namespace Day01;

public class Solver : Solver<CalibrationDocument, long>
{
public Solver() : base("./input.txt")
{
}

public override long PartOne(CalibrationDocument calibrationDocument)
{
return calibrationDocument.CalibrationValues
.Select(v =>
{
var digits = v.Where(char.IsDigit);
return long.Parse($"{digits.FirstOrDefault()}{digits.LastOrDefault()}");
}).Sum();
}

public override long PartTwo(CalibrationDocument calibrationDocument)
{
return calibrationDocument.CalibrationValues
.Select(RecoverCalibrationValue)
.Sum();

long RecoverCalibrationValue(string calibrationValue)
{
return long.Parse($"{FindFirstDigit(calibrationValue)}{FindFirstDigit(calibrationValue, RegexOptions.RightToLeft)}");
};

string FindFirstDigit(string calibrationValue, RegexOptions regexOptions = RegexOptions.None)
{
var pattern = @"\d|one|two|three|four|five|six|seven|eight|nine";
return ParseMatch(Regex.Match(calibrationValue, pattern, regexOptions).Value);
};

string ParseMatch(string match) => match switch
{
"one" => "1",
"two" => "2",
"three" => "3",
"four" => "4",
"five" => "5",
"six" => "6",
"seven" => "7",
"eight" => "8",
"nine" => "9",
_ => match
};
}

public override CalibrationDocument ParseInput(IEnumerable<string> input)
{
return new()
{
CalibrationValues = input.ToList()
};
}
}

public class CalibrationDocument
{
public required List<string> CalibrationValues { get; set; }
}
32 changes: 32 additions & 0 deletions src/Solutions/Day01/SolverTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using AdventOfCode.Commons;

namespace Day01;

public class SolverTest : TestEngine<Solver, CalibrationDocument, long>
{
public override Puzzle PartOne => new()
{
Example = new()
{
Input = new CalibrationDocument
{
CalibrationValues = ["1abc2", "pqr3stu8vwx", "a1b2c3d4e5f", "treb7uchet"]
},
Result = 142
},
Solution = 54159
};

public override Puzzle PartTwo => new()
{
Example = new()
{
Input = new CalibrationDocument
{
CalibrationValues = ["two1nine", "eightwothree", "abcone2threexyz", "xtwone3four", "4nineeightseven2", "zoneight234", "7pqrstsixteen"]
},
Result = 281
},
Solution = 53866
};
}
Loading

0 comments on commit f4920bd

Please sign in to comment.