diff --git a/06/WaitForIt.Tests/GlobalUsings.cs b/06/WaitForIt.Tests/GlobalUsings.cs
index 2d5b473..d6f0167 100644
--- a/06/WaitForIt.Tests/GlobalUsings.cs
+++ b/06/WaitForIt.Tests/GlobalUsings.cs
@@ -1,3 +1,3 @@
global using FluentAssertions;
-global using Xunit;
+global using Xunit;
\ No newline at end of file
diff --git a/06/WaitForIt/Program.cs b/06/WaitForIt/Program.cs
index e7fcf86..f422420 100644
--- a/06/WaitForIt/Program.cs
+++ b/06/WaitForIt/Program.cs
@@ -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(
/// The number of ways the race can be won.
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;
}
}
\ No newline at end of file
diff --git a/07/CamelCards.Tests/CamelCards.Tests.csproj b/07/CamelCards.Tests/CamelCards.Tests.csproj
new file mode 100644
index 0000000..9005157
--- /dev/null
+++ b/07/CamelCards.Tests/CamelCards.Tests.csproj
@@ -0,0 +1,36 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+ false
+ true
+
+
+
+
+
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+ all
+
+
+
+
+
+
+
+
+
+ PreserveNewest
+
+
+
+
diff --git a/07/CamelCards.Tests/CardTests.cs b/07/CamelCards.Tests/CardTests.cs
new file mode 100644
index 0000000..6a9daaf
--- /dev/null
+++ b/07/CamelCards.Tests/CardTests.cs
@@ -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();
+ }
+}
\ No newline at end of file
diff --git a/07/CamelCards.Tests/GlobalUsings.cs b/07/CamelCards.Tests/GlobalUsings.cs
new file mode 100644
index 0000000..2d5b473
--- /dev/null
+++ b/07/CamelCards.Tests/GlobalUsings.cs
@@ -0,0 +1,3 @@
+global using FluentAssertions;
+
+global using Xunit;
diff --git a/07/CamelCards.Tests/HandTests.cs b/07/CamelCards.Tests/HandTests.cs
new file mode 100644
index 0000000..6f75abc
--- /dev/null
+++ b/07/CamelCards.Tests/HandTests.cs
@@ -0,0 +1,204 @@
+namespace CamelCards.Tests;
+
+public class HandTests
+{
+ [Theory]
+ [MemberData(nameof(TestData.HandTypeTestData), MemberType = typeof(TestData))]
+ public void Type_WhenGivenListOfCards_ItShouldReturnExpectedHandType(List 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