feat: solve part 1

This commit is contained in:
Stevan Freeborn
2023-12-07 14:06:26 -06:00
parent 4621e17a8e
commit a84b0d07f0
9 changed files with 578 additions and 0 deletions
@@ -0,0 +1,36 @@
<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="..\CamelCards\CamelCards.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="..\INPUT.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
+31
View File
@@ -0,0 +1,31 @@
namespace CamelCards.Tests;
public class CardTests
{
[Theory]
[InlineData('A', 12)]
[InlineData('K', 11)]
[InlineData('Q', 10)]
[InlineData('J', 9)]
[InlineData('T', 8)]
[InlineData('9', 7)]
[InlineData('8', 6)]
[InlineData('7', 5)]
[InlineData('6', 4)]
[InlineData('5', 3)]
[InlineData('4', 2)]
[InlineData('3', 1)]
[InlineData('2', 0)]
public void Card_WhenGivenValidCharacter_ItShouldReturnCardWithExpectedStrength(char character, int expectedStrength)
{
var card = new Card(character);
card.Strength.Should().Be(expectedStrength);
}
[Fact]
public void Card_WhenGivenInvalidCardCharacter_ItShouldThrowArgumentException()
{
Action act = () => new Card('X');
act.Should().Throw<ArgumentException>();
}
}
+2
View File
@@ -0,0 +1,2 @@
global using Xunit;
global using FluentAssertions;
+204
View File
@@ -0,0 +1,204 @@
namespace CamelCards.Tests;
public class HandTests
{
[Theory]
[MemberData(nameof(TestData.HandTypeTestData), MemberType = typeof(TestData))]
public void Type_WhenGivenListOfCards_ItShouldReturnExpectedHandType(List<Card> cards, HandType expectedHandType)
{
var hand = new Hand(cards);
hand.Type.Should().Be(expectedHandType);
}
[Theory]
[MemberData(nameof(TestData.CompareToTestData), MemberType = typeof(TestData))]
public void CompareTo_WhenGivenHandWithHigherType_ItShouldReturnNegative1(Hand hand, Hand otherHand, int sortResult)
{
hand.CompareTo(otherHand).Should().Be(sortResult);
}
public static class TestData
{
public static IEnumerable<object[]> CompareToTestData =>
new List<object[]>
{
new object[]
{
new Hand(
[
new('A'),
new('A'),
new('A'),
new('A'),
new('A'),
]
),
new Hand(
[
new('A'),
new('A'),
new('8'),
new('A'),
new('A'),
]
),
1,
},
new object[]
{
new Hand(
[
new('A'),
new('A'),
new('8'),
new('A'),
new('A'),
]
),
new Hand(
[
new('A'),
new('A'),
new('A'),
new('A'),
new('A'),
]
),
-1,
},
new object[]
{
new Hand(
[
new('A'),
new('A'),
new('A'),
new('A'),
new('A'),
]
),
new Hand(
[
new('A'),
new('A'),
new('A'),
new('A'),
new('A'),
]
),
0,
},
new object[]
{
new Hand(
[
new('3'),
new('3'),
new('3'),
new('3'),
new('2'),
]
),
new Hand(
[
new('2'),
new('A'),
new('A'),
new('A'),
new('A'),
]
),
1,
}
};
public static IEnumerable<object[]> HandTypeTestData =>
new List<object[]>
{
new object[]
{
new List<Card>
{
new('A'),
new('A'),
new('A'),
new('A'),
new('A'),
},
HandType.FiveOfAKind,
},
new object[]
{
new List<Card>
{
new('A'),
new('A'),
new('8'),
new('A'),
new('A'),
},
HandType.FourOfAKind,
},
new object[]
{
new List<Card>
{
new('2'),
new('3'),
new('3'),
new('3'),
new('2'),
},
HandType.FullHouse,
},
new object[]
{
new List<Card>
{
new('T'),
new('T'),
new('T'),
new('9'),
new('8'),
},
HandType.ThreeOfAKind,
},
new object[]
{
new List<Card>
{
new('2'),
new('3'),
new('4'),
new('3'),
new('2'),
},
HandType.TwoPair,
},
new object[]
{
new List<Card>
{
new('A'),
new('2'),
new('3'),
new('A'),
new('4'),
},
HandType.OnePair,
},
new object[]
{
new List<Card>
{
new('2'),
new('3'),
new('4'),
new('5'),
new('6'),
},
HandType.HighCard,
},
};
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace CamelCards.Tests;
public class ProgramTests
{
[Fact]
public async Task Main_WhenCalledWithInputFile_ItShouldReturnExpectedResult()
{
var result = await Program.Main(["INPUT.txt"]);
result.Should().Be(251287184);
}
}
+95
View File
@@ -0,0 +1,95 @@
namespace CamelCards.Tests;
public class TurnTests
{
[Fact]
public void Parse_WhenGivenTurnInput_ItShouldReturnExpectedTurnInstance()
{
var turn = Turn.Parse("32T3K 765");
turn.Hand.Cards.Should().BeEquivalentTo(new List<Card>
{
new('3'),
new('2'),
new('T'),
new('3'),
new('K'),
});
turn.Bid.Should().Be(765);
}
[Fact]
public void ToString_WhenCalled_ItShouldReturnStringRepresentationOfTurn()
{
var turn = "32T3K 765";
Turn.Parse(turn).ToString().Should().Be(turn);
}
[Fact]
public void OrderBy_WhenGivenListOfTurns_ItShouldBeAbleToOrderThemAccordingToHandStrength()
{
var turns = TestData.TestTurns
.Select(Turn.Parse)
.ToList();
turns
.OrderBy(t => t.Hand)
.Select(t => t.ToString())
.Should()
.BeEquivalentTo(
[
"32T3K 765",
"KTJJT 220",
"KK677 28",
"T55J5 684",
"QQQJA 483",
]
);
}
[Fact]
public void OrderByDescending_WhenGivenListOfTurns_ItShouldBeAbleToOrderThemAccordingToHandStrength()
{
var turns = TestData.TestTurns
.Select(Turn.Parse)
.ToList();
turns
.OrderByDescending(t => t.Hand)
.Select(t => t.ToString())
.Should()
.BeEquivalentTo(
[
"QQQJA 483",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"32T3K 765",
]
);
}
[Fact]
public void Turn_GivenListOfTurns_ItShouldBeAbleToCalculateTotalWinnings()
{
TestData.TestTurns
.Select(Turn.Parse)
.OrderBy(t => t.Hand)
.Select((turn, index) => turn.Bid * (index + 1))
.Sum()
.Should()
.Be(6440);
}
public static class TestData
{
public static readonly string[] TestTurns =
[
"32T3K 765",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"QQQJA 483"
];
}
}