Skip to content

Commit

Permalink
Refactor to split unit tests out to separate project
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuo13 committed Jan 6, 2024
1 parent c6faf65 commit 663bec9
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 71 deletions.
8 changes: 7 additions & 1 deletion AdventOfCode.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode", "AdventOfCode\AdventOfCode.csproj", "{B4A4D1C2-B2FD-4A90-9A3C-7F8D3DC1D212}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventOfCode", "src\AdventOfCode\AdventOfCode.csproj", "{B4A4D1C2-B2FD-4A90-9A3C-7F8D3DC1D212}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.Tests", "tests\AdventOfCode.Tests\AdventOfCode.Tests.csproj", "{6AC23A70-AF1E-4F60-86F1-39A4F1310E6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{B4A4D1C2-B2FD-4A90-9A3C-7F8D3DC1D212}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4A4D1C2-B2FD-4A90-9A3C-7F8D3DC1D212}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4A4D1C2-B2FD-4A90-9A3C-7F8D3DC1D212}.Release|Any CPU.Build.0 = Release|Any CPU
{6AC23A70-AF1E-4F60-86F1-39A4F1310E6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AC23A70-AF1E-4F60-86F1-39A4F1310E6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AC23A70-AF1E-4F60-86F1-39A4F1310E6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AC23A70-AF1E-4F60-86F1-39A4F1310E6E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Solutions to the [Advent of Code](https://adventofcode.com/) puzzles.

| Puzzle | Solution | Date | Notes |
| ------ | -------- | ---- | ----- |
| [Trebuchet?!](https://adventofcode.com/2023/day/1) | [Solution](./AdventOfCode/Calendar/2023/Day01/Solution.cs) | 2023-01 | Completed |
| [Cube Conundrum](https://adventofcode.com/2023/day/2) | [Solution](./AdventOfCode/Calendar/2023/Day02/Solution.cs) | 2023-02 | Completed |
| [Gear Ratios](https://adventofcode.com/2023/day/3) | [Solution](./AdventOfCode/Calendar/2023/Day03/Solution.cs) | 2023-03 | Incomplete |
| [Scratchcards](https://adventofcode.com/2023/day/4) | [Solution](./AdventOfCode/Calendar/2023/Day04/Solution.cs) | 2023-04 | Completed |
| [Trebuchet?!](https://adventofcode.com/2023/day/1) | [Solution](./src/AdventOfCode/Calendar/2023/Day01/Solution.cs) | 2023-01 | Completed |
| [Cube Conundrum](https://adventofcode.com/2023/day/2) | [Solution](./src/AdventOfCode/Calendar/2023/Day02/Solution.cs) | 2023-02 | Completed |
| [Gear Ratios](https://adventofcode.com/2023/day/3) | [Solution](./src/AdventOfCode/Calendar/2023/Day03/Solution.cs) | 2023-03 | Incomplete |
| [Scratchcards](https://adventofcode.com/2023/day/4) | [Solution](./src/AdventOfCode/Calendar/2023/Day04/Solution.cs) | 2023-04 | Completed |
16 changes: 16 additions & 0 deletions src/AdventOfCode/AdventOfCode.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<None Update="Calendar\2023\*\input.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AdventOfCode.Calendar._2023.Day01;

internal class Solution : BaseSolution
public class Solution : BaseSolution
{
private static readonly Dictionary<string, int> _numberWords = new()
{
Expand Down Expand Up @@ -32,11 +32,8 @@ public override async Task<int> Run(RunMode runMode)
/// represent the tens place. The last number will be the ones place.
/// </summary>
private static int SumCalibrationValues(string[] inputLines) =>
inputLines.Sum(line =>
{
return 10 * AsNumber(line.First(char.IsDigit)) +
AsNumber(line.Last(char.IsDigit));
});
inputLines.Sum(line => 10 * AsNumber(line.First(char.IsDigit)) +
AsNumber(line.Last(char.IsDigit)));

/// <summary>
/// Make two passes against the line: forward from beginning to end looking
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions src/AdventOfCode/Calendar/2023/Day02/SetOfCubes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Reflection;

namespace AdventOfCode.Calendar._2023.Day02;

public sealed class SetOfCubes()
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }

#pragma warning disable S1144 // Unused private types or members should be removed
public int this[string colorName]
#pragma warning restore S1144 // Unused private types or members should be removed
{
set
{
GetType()
.GetProperty(colorName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public)
!.SetValue(this, value, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Reflection;
namespace AdventOfCode.Calendar._2023.Day02;

namespace AdventOfCode.Calendar._2023.Day02;

internal class Solution : BaseSolution
public class Solution : BaseSolution
{
private readonly SetOfCubes _constraint = new()
{
Expand All @@ -11,25 +9,6 @@ internal class Solution : BaseSolution
Blue = 14
};

private sealed class SetOfCubes()
{
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }

#pragma warning disable S1144 // Unused private types or members should be removed
public int this[string colorName]
#pragma warning restore S1144 // Unused private types or members should be removed
{
set
{
GetType()
.GetProperty(colorName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public)
!.SetValue(this, value, null);
}
}
}

public override async Task<int> Run(RunMode runMode)
{
var lines = await ReadInput();
Expand Down Expand Up @@ -65,9 +44,9 @@ private static int SumPowerSets(string[] lines)
{
var sum = 0;

for (var gameId = 0; gameId < lines.Length; gameId++)
foreach (var line in lines)
{
var sets = SetsInGame(lines[gameId]);
var sets = SetsInGame(line);

sum += sets.Max(x => x.Red) * sets.Max(x => x.Green) * sets.Max(x => x.Blue);
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AdventOfCode.Calendar._2023.Day03;
/// than implementing algorithms from scratch [poorly] -- this really showed
/// for part 2.
/// </summary>
internal partial class Solution : BaseSolution
public partial class Solution : BaseSolution
{
private const char PeriodSymbol = '.';
private const char GearSymbol = '*';
Expand Down Expand Up @@ -74,7 +74,7 @@ private int SumGearRatios()
_lines[lineNumber][match.Index - 2] != PeriodSymbol &&
!seen.Exists(x => x.Value == match.Value))
{
var rl = GearRatioRLRegex();
var rl = GearRatioRightLeftRegex();
var rlMatches = rl.Match(_lines[lineNumber], match.Index);

if (rlMatches.Success)
Expand All @@ -89,7 +89,7 @@ private int SumGearRatios()
if (boundingBox[(int)Direction.Right].Contains(GearSymbol) &&
_lines[lineNumber][match.Index + match.Value.Length + 1] != PeriodSymbol)
{
var lr = GearRatioLRRegex();
var lr = GearRatioLeftRightRegex();
var rlMatches = lr.Match(_lines[lineNumber], match.Index + match.Value.Length);

if (rlMatches.Success)
Expand All @@ -110,8 +110,8 @@ private int SumGearRatios()
// to the right and left of it then concatenate them to
// get the part number.

var lr = GearRatioLRRegex();
var rl = GearRatioRLRegex();
var lr = GearRatioLeftRightRegex();
var rl = GearRatioRightLeftRegex();
var lrMatches = lr.Match(_lines[lineNumber + 2], match.Index + gearPosition);
var rlMatches = rl.Match(_lines[lineNumber + 2], match.Index + gearPosition);

Expand Down Expand Up @@ -199,8 +199,8 @@ private string[] GetBoundingBox(int currentLine, int matchIndex, string number)
private static partial Regex NumberRegex();

[GeneratedRegex(@"\d+")]
private static partial Regex GearRatioLRRegex();
private static partial Regex GearRatioLeftRightRegex();

[GeneratedRegex(@"\d+", RegexOptions.RightToLeft)]
private static partial Regex GearRatioRLRegex();
private static partial Regex GearRatioRightLeftRegex();
}
File renamed without changes.
8 changes: 8 additions & 0 deletions src/AdventOfCode/Calendar/2023/Day04/Scratchcard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AdventOfCode.Calendar._2023.Day04;

public class Scratchcard
{
public IEnumerable<int> WinningNumbers { get; set; } = Enumerable.Empty<int>();
public IEnumerable<int> MyNumbers { get; set; } = Enumerable.Empty<int>();
public int Worth => (int)Math.Pow(2, WinningNumbers.Count() - 1);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
namespace AdventOfCode.Calendar._2023.Day04;

internal class Solution : BaseSolution
public class Solution : BaseSolution
{
private class Scratchcard
{
public IEnumerable<int> WinningNumbers { get; set; } = Enumerable.Empty<int>();
public IEnumerable<int> MyNumbers { get; set; } = Enumerable.Empty<int>();
public int Worth => (int)Math.Pow(2, WinningNumbers.Count() - 1);
}

public override async Task<int> Run(RunMode runMode)
{
var scratchcards = await CountCards();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class BaseSolution
/// Input file will reside in a subdirectory of the executable. That
/// path will follow the same as the namespace mostly.
/// </summary>
public virtual async Task<string[]> ReadInput()
protected virtual async Task<string[]> ReadInput()
{
var solutionDirectory = GetType().Namespace!
.Replace(nameof(AdventOfCode), string.Empty)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Update="Calendar\2023\*\input.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<ProjectReference Include="..\..\src\AdventOfCode\AdventOfCode.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Xunit;
using AdventOfCode.Calendar._2023.Day01;
using Xunit;

namespace AdventOfCode.Calendar._2023.Day01;
namespace AdventOfCode.Tests.Calendar._2023.Day01;

public class SolutionTests
{
private class TestSolution(string[] inputLines) : Solution
{
public override Task<string[]> ReadInput() => Task.FromResult(inputLines);
protected override Task<string[]> ReadInput() => Task.FromResult(inputLines);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Xunit;
using AdventOfCode.Calendar._2023.Day02;
using Xunit;

namespace AdventOfCode.Calendar._2023.Day02;
namespace AdventOfCode.Tests.Calendar._2023.Day02;

public class SolutionTests
{
private class TestSolution(string[] inputLines) : Solution
{
public override Task<string[]> ReadInput() => Task.FromResult(inputLines);
protected override Task<string[]> ReadInput() => Task.FromResult(inputLines);
}

private readonly string[] _input =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Xunit;
using AdventOfCode.Calendar._2023.Day03;
using Xunit;

namespace AdventOfCode.Calendar._2023.Day03;
namespace AdventOfCode.Tests.Calendar._2023.Day03;

public class SolutionTests
{
private class TestSolution(string[] inputLines) : Solution
{
public override Task<string[]> ReadInput() => Task.FromResult(inputLines);
protected override Task<string[]> ReadInput() => Task.FromResult(inputLines);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Xunit;
using AdventOfCode.Calendar._2023.Day04;
using Xunit;

namespace AdventOfCode.Calendar._2023.Day04;
namespace AdventOfCode.Tests.Calendar._2023.Day04;

public class SolutionTests
{
private class TestSolution(string[] inputLines) : Solution
{
public override Task<string[]> ReadInput() => Task.FromResult(inputLines);
protected override Task<string[]> ReadInput() => Task.FromResult(inputLines);
}

[Theory]
Expand Down

0 comments on commit 663bec9

Please sign in to comment.