feat: solve part 1
This commit is contained in:
@@ -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>
|
||||
@@ -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>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
global using Xunit;
|
||||
global using FluentAssertions;
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,173 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace CamelCards;
|
||||
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
if (args.Length is 0)
|
||||
{
|
||||
Console.WriteLine("Please provide a path to the input file.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (File.Exists(args[0]) is false)
|
||||
{
|
||||
Console.WriteLine("The provided file does not exist.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
var input = await File.ReadAllLinesAsync(args[0]);
|
||||
|
||||
var stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
|
||||
var result = input
|
||||
.Select(Turn.Parse)
|
||||
.OrderBy(t => t.Hand)
|
||||
.Select((turn, index) => turn.Bid * (index + 1))
|
||||
.Sum();
|
||||
|
||||
stopWatch.Stop();
|
||||
|
||||
Console.WriteLine($"The total winnings is {result}. ({stopWatch.ElapsedMilliseconds}ms)");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Turn(
|
||||
Hand hand,
|
||||
int bid
|
||||
)
|
||||
{
|
||||
public Hand Hand { get; init; } = hand;
|
||||
public int Bid { get; init; } = bid;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{string.Join("", Hand.Cards.Select(c => c.Value))} {Bid}";
|
||||
}
|
||||
|
||||
public static Turn Parse(string turnInput)
|
||||
{
|
||||
var parts = turnInput.Split(' ', StringSplitOptions.TrimEntries);
|
||||
var cards = parts[0].Select(c => new Card(c)).ToList();
|
||||
var bid = int.Parse(parts[1]);
|
||||
|
||||
return new Turn(new(cards), bid);
|
||||
}
|
||||
}
|
||||
|
||||
public class Hand : IComparable<Hand>
|
||||
{
|
||||
public List<Card> Cards { get; init; }
|
||||
|
||||
public Hand(List<Card> cards)
|
||||
{
|
||||
if (cards.Count != 5)
|
||||
{
|
||||
throw new ArgumentException("A hand must have 5 cards");
|
||||
}
|
||||
|
||||
Cards = cards;
|
||||
}
|
||||
|
||||
public HandType Type => Cards.GroupBy(c => c.Value).Count() switch
|
||||
{
|
||||
5 => HandType.HighCard,
|
||||
4 => HandType.OnePair,
|
||||
3 => Cards.GroupBy(c => c.Value).Any(g => g.Count() == 3) ? HandType.ThreeOfAKind : HandType.TwoPair,
|
||||
2 => Cards.GroupBy(c => c.Value).Any(g => g.Count() == 4) ? HandType.FourOfAKind : HandType.FullHouse,
|
||||
1 => HandType.FiveOfAKind,
|
||||
_ => throw new ApplicationException("Hand has no defined type"),
|
||||
};
|
||||
|
||||
public int CompareTo(Hand? other)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (other == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
result = Type.CompareTo(other.Type);
|
||||
|
||||
if (result != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
for (int i = 0; i < Cards.Count; i++)
|
||||
{
|
||||
var currentCard = Cards[i];
|
||||
var otherCard = other.Cards[i];
|
||||
|
||||
var cardComparison = currentCard.Strength.CompareTo(otherCard.Strength);
|
||||
|
||||
if (i == Cards.Count - 1)
|
||||
{
|
||||
result = cardComparison;
|
||||
}
|
||||
|
||||
if (cardComparison != 0)
|
||||
{
|
||||
result = cardComparison;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public enum HandType
|
||||
{
|
||||
HighCard,
|
||||
OnePair,
|
||||
TwoPair,
|
||||
ThreeOfAKind,
|
||||
FullHouse,
|
||||
FourOfAKind,
|
||||
FiveOfAKind,
|
||||
}
|
||||
|
||||
|
||||
public class Card
|
||||
{
|
||||
private static readonly Dictionary<char, int> Cards = new()
|
||||
{
|
||||
['A'] = 12,
|
||||
['K'] = 11,
|
||||
['Q'] = 10,
|
||||
['J'] = 9,
|
||||
['T'] = 8,
|
||||
['9'] = 7,
|
||||
['8'] = 6,
|
||||
['7'] = 5,
|
||||
['6'] = 4,
|
||||
['5'] = 3,
|
||||
['4'] = 2,
|
||||
['3'] = 1,
|
||||
['2'] = 0,
|
||||
};
|
||||
|
||||
public char Value { get; init; }
|
||||
public int Strength { get; init; }
|
||||
|
||||
public Card(char value)
|
||||
{
|
||||
var isValidCardChar = Cards.TryGetValue(value, out var strength);
|
||||
|
||||
if (!isValidCardChar)
|
||||
{
|
||||
throw new ArgumentException($"Invalid card value: {value}");
|
||||
}
|
||||
|
||||
Value = value;
|
||||
Strength = strength;
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaitForIt", "06\WaitForIt\W
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaitForIt.Tests", "06\WaitForIt.Tests\WaitForIt.Tests.csproj", "{8C89E1D0-2617-4B89-BA9C-9189FC69BC43}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "07", "07", "{7F8AD027-D8D7-407B-9E08-B363B7B34621}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CamelCards", "07\CamelCards\CamelCards.csproj", "{DC16CBE1-B1FA-4E6B-AAD8-43CDECA46659}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CamelCards.Tests", "07\CamelCards.Tests\CamelCards.Tests.csproj", "{8F04A78E-D2E1-41AC-B43C-74608B7B4FCB}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -96,6 +102,14 @@ Global
|
||||
{8C89E1D0-2617-4B89-BA9C-9189FC69BC43}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8C89E1D0-2617-4B89-BA9C-9189FC69BC43}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8C89E1D0-2617-4B89-BA9C-9189FC69BC43}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DC16CBE1-B1FA-4E6B-AAD8-43CDECA46659}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DC16CBE1-B1FA-4E6B-AAD8-43CDECA46659}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DC16CBE1-B1FA-4E6B-AAD8-43CDECA46659}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DC16CBE1-B1FA-4E6B-AAD8-43CDECA46659}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8F04A78E-D2E1-41AC-B43C-74608B7B4FCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8F04A78E-D2E1-41AC-B43C-74608B7B4FCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8F04A78E-D2E1-41AC-B43C-74608B7B4FCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8F04A78E-D2E1-41AC-B43C-74608B7B4FCB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{3F8EAC09-4BC7-43AA-B72B-48DDD2710F6D} = {8C29858C-623A-461A-BF0B-254E151CD9C2}
|
||||
@@ -110,5 +124,7 @@ Global
|
||||
{CB90004D-9420-4411-9110-B50274537FE5} = {5067FD6B-8F03-4502-AB83-8A391D801B47}
|
||||
{B3D736D4-EFEE-44AA-B199-1A1C803F1793} = {86704890-D6B8-4166-853B-F1424B8396C7}
|
||||
{8C89E1D0-2617-4B89-BA9C-9189FC69BC43} = {86704890-D6B8-4166-853B-F1424B8396C7}
|
||||
{DC16CBE1-B1FA-4E6B-AAD8-43CDECA46659} = {7F8AD027-D8D7-407B-9E08-B363B7B34621}
|
||||
{8F04A78E-D2E1-41AC-B43C-74608B7B4FCB} = {7F8AD027-D8D7-407B-9E08-B363B7B34621}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
Reference in New Issue
Block a user