feat: solve puzzle 3
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<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="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
|
||||
<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="6.0.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GearRatios\GearRatios.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,2 @@
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
@@ -0,0 +1,353 @@
|
||||
namespace GearRatios.Test;
|
||||
|
||||
public class LineTests
|
||||
{
|
||||
[Theory, MemberData(nameof(TestData.LineData), MemberType = typeof(TestData))]
|
||||
public void Parse_GivenLine_ItShouldParseExpectedValues(string line, Line expected)
|
||||
{
|
||||
var actual = Line.Parse(line);
|
||||
actual.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Theory, MemberData(nameof(TestData.PartNumberData), MemberType = typeof(TestData))]
|
||||
public void GetPartNumbers_GivenLine_ItShouldReturnExpectedValues(Line currentLine, Line? prevLine, Line? nextLine, List<int> expected)
|
||||
{
|
||||
var actual = currentLine.GetPartNumbers(prevLine, nextLine);
|
||||
actual.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Theory, MemberData(nameof(TestData.GearData), MemberType = typeof(TestData))]
|
||||
public void GetGearRatios_GivenLine_ItShouldReturnExpectedValues(Line currentLine, Line? prevLine, Line? nextLine, List<int> expected)
|
||||
{
|
||||
var actual = currentLine.GetGearRatios(prevLine, nextLine);
|
||||
actual.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static IEnumerable<object?[]> GearData =>
|
||||
new List<object?[]>
|
||||
{
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[],
|
||||
[]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int>(),
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 467, new Indexes(0, 2) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 114, new Indexes(5, 7) },
|
||||
},
|
||||
],
|
||||
[
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int>(),
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int>(),
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 617, new Indexes(0, 2) },
|
||||
{ 114, new Indexes(4, 6) },
|
||||
},
|
||||
],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int> { 617 * 114 },
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 467, new Indexes(0, 2) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 114, new Indexes(5, 7) },
|
||||
},
|
||||
],
|
||||
[]
|
||||
),
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 35, new Indexes(2, 3) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 633, new Indexes(6, 8) },
|
||||
},
|
||||
],
|
||||
[]
|
||||
),
|
||||
new List<int> { 467 * 35 },
|
||||
}
|
||||
};
|
||||
|
||||
public static IEnumerable<object?[]> PartNumberData =>
|
||||
new List<object?[]>
|
||||
{
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 467, new Indexes(0, 2) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 114, new Indexes(5, 7) },
|
||||
},
|
||||
],
|
||||
[
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int>(),
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int>(),
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 617, new Indexes(0, 2) },
|
||||
},
|
||||
],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int> { 617 },
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 617, new Indexes(0, 2) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 114, new Indexes(6, 8) },
|
||||
},
|
||||
],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(9, 9) },
|
||||
},
|
||||
]
|
||||
),
|
||||
null,
|
||||
null,
|
||||
new List<int> { 617, 114 },
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 467, new Indexes(0, 2) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 114, new Indexes(5, 7) },
|
||||
},
|
||||
],
|
||||
[
|
||||
]
|
||||
),
|
||||
null,
|
||||
new Line(
|
||||
[],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
new List<int>() { 467 },
|
||||
},
|
||||
new object?[]
|
||||
{
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 58, new Indexes(7, 8) },
|
||||
},
|
||||
],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "+", new Indexes(5, 5) },
|
||||
},
|
||||
]
|
||||
),
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 617, new Indexes(0, 2) },
|
||||
},
|
||||
],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 592, new Indexes(2, 4) },
|
||||
},
|
||||
],
|
||||
[]
|
||||
),
|
||||
new List<int>(),
|
||||
},
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> LineData =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
"467..114..",
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 467, new Indexes(0, 2) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 114, new Indexes(5, 7) },
|
||||
},
|
||||
],
|
||||
[]
|
||||
),
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
"...*......",
|
||||
new Line(
|
||||
[],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "*", new Indexes(3, 3) },
|
||||
},
|
||||
]
|
||||
),
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
"...41+...564",
|
||||
new Line(
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ 41, new Indexes(3, 4) },
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ 564, new Indexes(9, 11) },
|
||||
},
|
||||
],
|
||||
[
|
||||
new()
|
||||
{
|
||||
{ "+", new Indexes(5, 5) },
|
||||
},
|
||||
]
|
||||
),
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
namespace GearRatios.Test;
|
||||
|
||||
public class PuzzleSolverTests
|
||||
{
|
||||
private readonly PuzzleSolver _sut = new();
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.SumPartNumbersData), MemberType = typeof(TestData))]
|
||||
public void SumPartNumbers_GivenSchematic_ItShouldReturnExpectedValue(string[] schematic, int expected)
|
||||
{
|
||||
var actual = _sut.SumPartNumbers(schematic);
|
||||
actual.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.SumGearRatios), MemberType = typeof(TestData))]
|
||||
public void SumGearRatios_GivenSchematic_ItShouldReturnExpectedValue(string[] schematic, int expected)
|
||||
{
|
||||
var actual = _sut.SumGearRatios(schematic);
|
||||
actual.Should().Be(expected);
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
private static readonly string[] TestSchematic =
|
||||
[
|
||||
"467..114..",
|
||||
"...*......",
|
||||
"..35..633.",
|
||||
"......#...",
|
||||
"617*......",
|
||||
".....+.58.",
|
||||
"..592.....",
|
||||
"......755.",
|
||||
"...$.*....",
|
||||
".664.598..",
|
||||
];
|
||||
|
||||
public static IEnumerable<object[]> SumGearRatios =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
TestSchematic,
|
||||
467835,
|
||||
},
|
||||
};
|
||||
|
||||
public static IEnumerable<object?[]> SumPartNumbersData =>
|
||||
new List<object?[]>
|
||||
{
|
||||
new object?[]
|
||||
{
|
||||
TestSchematic,
|
||||
4361,
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new[]
|
||||
{
|
||||
"...................305.124................................432..............................................576..313.....514.................",
|
||||
".............113...-......&....................&...819...........654..../..........................&901................*....869.257.........",
|
||||
"...377..&783../.................................9...........855*......940..463................-.........................844.*....@......679.",
|
||||
"......*...........197.261.....817..336.759............&742......548.......&........748......844.............#.......&........254...169..*...",
|
||||
},
|
||||
10421,
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new[]
|
||||
{
|
||||
".............................457....834.....................94..564..........&.........498....*...304....../...*.....*..........845.....&...",
|
||||
".......12*48.753...244..196....=......$..721....90*29.........*.................+.522.......487...............317.....531..311........20....",
|
||||
".177...............*.....*.................*.............*969.611.......*565..338...*.............712..922$.......770......*....522.........",
|
||||
},
|
||||
8318
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new[]
|
||||
{
|
||||
"663..462...........*..........109..706*.................=.............553........712......*971......674.396...635*..........................",
|
||||
"........../...728.952...413......*......744.......%..........................300..*....782................*.......742..&424........41+...564",
|
||||
"........375...%.........*......450.456.$.........714........851.327..#...+......*...+.......179.630....854.................................*",
|
||||
},
|
||||
11612
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user