Merge pull request #7 from StevanFreeborn/stevanfreeborn/feat/solve-puzzle-7

feat: solve puzzle 7
This commit is contained in:
Stevan Freeborn
2023-12-07 19:35:51 -06:00
committed by GitHub
12 changed files with 757 additions and 20 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
global using FluentAssertions;
global using Xunit;
global using Xunit;
+27 -10
View File
@@ -1,4 +1,6 @@
namespace WaitForIt;
using System.Diagnostics;
namespace WaitForIt;
public class Program
{
@@ -20,6 +22,9 @@ public class Program
var input = await File.ReadAllLinesAsync(args[0]);
var isPart2 = args.Length > 1 && args[1] == "part2";
var stopWatch = new Stopwatch();
stopWatch.Start();
long result = isPart2
? parser
.ParseRace(input)
@@ -28,13 +33,15 @@ public class Program
.Select(r => r.CalculateNumberOfWaysToWin())
.Aggregate((long)1, (acc, curr) => acc * curr);
stopWatch.Stop();
if (isPart2)
{
Console.WriteLine($"The number of ways to win is {result}.");
Console.WriteLine($"The number of ways to win is {result}. ({stopWatch.ElapsedMilliseconds}ms)");
}
else
{
Console.WriteLine($"The total number of ways to win is {result}.");
Console.WriteLine($"The total number of ways to win is {result}. ({stopWatch.ElapsedMilliseconds}ms)");
}
return (int)result;
@@ -117,19 +124,29 @@ public class Race(
/// <returns>The number of ways the race can be won.</returns>
public long CalculateNumberOfWaysToWin()
{
var numberOfWaysToWin = 0;
var minDuration = 0.0;
var maxDuration = Math.Floor(Duration / 2.0);
for (var secsHeld = 0; secsHeld < Duration; secsHeld++)
while (minDuration < maxDuration - 1)
{
var speed = 1 * secsHeld;
var distance = speed * (Duration - secsHeld);
var middleDuration = Math.Floor((maxDuration + minDuration) / 2);
var speed = 1 * middleDuration;
var distance = speed * (Duration - middleDuration);
if (distance > DistanceRecord)
if (distance >= DistanceRecord)
{
numberOfWaysToWin++;
maxDuration = middleDuration;
}
else
{
minDuration = middleDuration;
}
}
return numberOfWaysToWin;
var result = Duration % 2 == 0
? Duration - ((long)maxDuration * 2) - 1
: Duration - ((long)maxDuration * 2) + 1;
return result;
}
}
@@ -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>
+32
View File
@@ -0,0 +1,32 @@
namespace CamelCards.Tests;
public class CardTests
{
[Theory]
[InlineData('A', 13)]
[InlineData('K', 12)]
[InlineData('Q', 11)]
[InlineData('J', 10)]
[InlineData('T', 9)]
[InlineData('9', 8)]
[InlineData('8', 7)]
[InlineData('7', 6)]
[InlineData('6', 5)]
[InlineData('5', 4)]
[InlineData('4', 3)]
[InlineData('3', 2)]
[InlineData('2', 1)]
[InlineData('W', 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>();
}
}
+3
View File
@@ -0,0 +1,3 @@
global using FluentAssertions;
global using Xunit;
+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,
},
};
}
}
+18
View File
@@ -0,0 +1,18 @@
namespace CamelCards.Tests;
public class ProgramTests
{
[Fact]
public async Task Main_WhenCalledWithInputFile_ItShouldReturnExpectedResult()
{
var result = await Program.Main(["INPUT.txt"]);
result.Should().Be(251287184);
}
[Fact]
public async Task Main_WhenCalledWithInputFileAsPart2_ItShouldReturnExpectedResult()
{
var result = await Program.Main(["INPUT.txt", "part2"]);
result.Should().Be(250757288);
}
}
+198
View File
@@ -0,0 +1,198 @@
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(t => Turn.Parse(t))
.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(t => Turn.Parse(t))
.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 OrderBy_WhenGivenListOfTurnsAndJokersAreTreatedAsWild_ItShouldBeAbleToOrderThemAccordingToHandStrength()
{
var turns = TestData.TestTurns
.Select(t => Turn.Parse(t, true))
.ToList();
turns
.OrderBy(t => t.Hand)
.Select(t => t.ToString())
.Should()
.BeEquivalentTo(
[
"32T3K 765",
"KK677 28",
"T55J5 684",
"QQQJA 483",
"KTJJT 220",
]
);
}
[Fact]
public void Turn_GivenListOfTurns_ItShouldBeAbleToCalculateTotalWinnings()
{
TestData.TestTurns
.Select(t => Turn.Parse(t))
.OrderBy(t => t.Hand)
.Select((turn, index) => turn.Bid * (index + 1))
.Sum()
.Should()
.Be(6440);
}
[Fact]
public void Turn_GivenListOfTurnsAndJokersAreWild_ItShouldBeAbleToCalculateTotalWinnings()
{
TestData.TestTurns
.Select(t => Turn.Parse(t, true))
.OrderBy(t => t.Hand)
.Select((turn, index) => turn.Bid * (index + 1))
.Sum()
.Should()
.Be(5905);
}
[Theory]
[MemberData(nameof(TestData.TestTurnsWithJokersData), MemberType = typeof(TestData))]
public void Turn_GivenListOfTurnsWithJokers_ItShouldHaveCorrectHandType(string turn, HandType expectedHandType)
{
Turn.Parse(turn, true).Hand.Type.Should().Be(expectedHandType);
}
public static class TestData
{
public static readonly string[] InputTurnsWithJokers = File.ReadAllLines("INPUT.txt").Where(l => l.Contains('J')).ToArray();
public static IEnumerable<object[]> TestTurnsWithJokersData =>
new List<object[]>
{
new object[]
{
"4446J 425",
HandType.FourOfAKind,
},
new object[]
{
"26J93 60",
HandType.OnePair,
},
new object[]
{
"TQ9JQ 554",
HandType.ThreeOfAKind,
},
new object[]
{
"J373A 525",
HandType.ThreeOfAKind,
},
new object[]
{
"44JJ4 738",
HandType.FiveOfAKind,
},
new object[]
{
"JTK95 684",
HandType.OnePair,
},
new object[]
{
"5J39Q 743",
HandType.OnePair,
},
new object[]
{
"222J2 833",
HandType.FiveOfAKind,
},
new object[]
{
"JJJ44 668",
HandType.FiveOfAKind,
},
new object[]
{
"4JK47 317",
HandType.ThreeOfAKind,
},
new object[]
{
"66J4J 253",
HandType.FourOfAKind,
}
};
public static readonly string[] TestTurns =
[
"32T3K 765",
"T55J5 684",
"KK677 28",
"KTJJT 220",
"QQQJA 483"
];
}
}
+10
View File
@@ -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>
+203
View File
@@ -0,0 +1,203 @@
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 jokersWild = args.Length > 1 && args[1] == "part2";
var input = await File.ReadAllLinesAsync(args[0]);
var stopWatch = new Stopwatch();
stopWatch.Start();
var result = input
.Select(line => Turn.Parse(line, jokersWild))
.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()
{
var cards = Hand.Cards
.Select(c =>
{
return c.Value == 'W'
? 'J'
: c.Value;
});
return $"{string.Join("", cards)} {Bid}";
}
public static Turn Parse(string turnInput, bool jokersWild = false)
{
var parts = turnInput.Split(' ', StringSplitOptions.TrimEntries);
var cards = parts[0]
.Select(c => jokersWild && c == 'J' ? new Card('W') : 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.Any(c => c.Value == 'W')
? Cards
.GroupBy(c => c.Value)
.Count() switch
{
5 => HandType.OnePair,
4 => HandType.ThreeOfAKind,
3 => Cards.GroupBy(c => c.Value).Any(g => g.Count() == 3)
? HandType.FourOfAKind
: Cards.Count(c => c.Value == 'W') == 2
? HandType.FourOfAKind
: HandType.FullHouse,
2 or
1 => HandType.FiveOfAKind,
_ => throw new ApplicationException("Hand has no defined 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'] = 13,
['K'] = 12,
['Q'] = 11,
['J'] = 10,
['T'] = 9,
['9'] = 8,
['8'] = 7,
['7'] = 6,
['6'] = 5,
['5'] = 4,
['4'] = 3,
['3'] = 2,
['2'] = 1,
['W'] = 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;
}
}
+16
View File
@@ -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
+9 -9
View File
@@ -42,15 +42,15 @@ dotnet build
## Challenges
| Day | Problem | Solution | Status | Notes |
| --- | -------------------------- | :-----------------------------: | :----: | ------------------------------------------------------------------- |
| 01 | [Problem](./01/PROBLEM.md) | [Solution](./01/Trebuchet/) | ✅ | The trickiest part here was accounting for overlapping digit words. |
| 02 | [Problem](./02/PROBLEM.md) | [Solution](./02/CubeConundrum/) | ✅ | The key to me here was to parse the input into a useful model. |
| 03 | [Problem](./03/PROBLEM.md) | [Solution](./03/GearRatios/) | ✅ | The edge case that got me here was lines ending with a part number. |
| 04 | [Problem](./04/PROBLEM.md) | [Solution](./04/Scratchcards/) | ✅ | Part 2 gets out of hand quickly with just 200 cards. |
| 05 | [Problem](./05/PROBLEM.md) | [Solution](./05/IYGASAF/) | ✅ | I brute forced part 2 using parallelism. I know shame. |
| 06 | [Problem](./06/PROBLEM.md) | [Solution](./06/WaitForIt/) | ✅ | Thank goodness part 2 was not like 5's part 2. 😅 |
| 07 | [Problem](./07/PROBLEM.md) | [Solution](./07/) | |
| Day | Problem | Solution | Status | Notes |
| --- | -------------------------- | :-----------------------------: | :----: | ------------------------------------------------------------------------------------------------------- |
| 01 | [Problem](./01/PROBLEM.md) | [Solution](./01/Trebuchet/) | ✅ | The trickiest part here was accounting for overlapping digit words. |
| 02 | [Problem](./02/PROBLEM.md) | [Solution](./02/CubeConundrum/) | ✅ | The key to me here was to parse the input into a useful model. |
| 03 | [Problem](./03/PROBLEM.md) | [Solution](./03/GearRatios/) | ✅ | The edge case that got me here was lines ending with a part number. |
| 04 | [Problem](./04/PROBLEM.md) | [Solution](./04/Scratchcards/) | ✅ | Part 2 gets out of hand quickly with just 200 cards. |
| 05 | [Problem](./05/PROBLEM.md) | [Solution](./05/IYGASAF/) | ✅ | I brute forced part 2 using parallelism. I know shame. |
| 06 | [Problem](./06/PROBLEM.md) | [Solution](./06/WaitForIt/) | ✅ | Thank goodness part 2 was not like 5's part 2. 😅 |
| 07 | [Problem](./07/PROBLEM.md) | [Solution](./07/CamelCards/) | | What took me longest here was I missed a case when jokers are wild and there are three groups of cards. |
| 08 | [Problem](./08/PROBLEM.md) | [Solution](./08/) | ⌛ |
| 09 | [Problem](./09/PROBLEM.md) | [Solution](./09/) | ⌛ |
| 10 | [Problem](./10/PROBLEM.md) | [Solution](./10/) | ⌛ |