From 4621e17a8eb3e3a8faef87bc3c04b0764706f08a Mon Sep 17 00:00:00 2001
From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com>
Date: Wed, 6 Dec 2023 23:23:47 -0600
Subject: [PATCH 1/4] chore: add stopwatch
---
06/WaitForIt/Program.cs | 37 +++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)
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
From a84b0d07f063fc2e013c3c71af196d40a526c82b Mon Sep 17 00:00:00 2001
From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com>
Date: Thu, 7 Dec 2023 14:06:26 -0600
Subject: [PATCH 2/4] feat: solve part 1
---
07/CamelCards.Tests/CamelCards.Tests.csproj | 36 ++++
07/CamelCards.Tests/CardTests.cs | 31 +++
07/CamelCards.Tests/GlobalUsings.cs | 2 +
07/CamelCards.Tests/HandTests.cs | 204 ++++++++++++++++++++
07/CamelCards.Tests/ProgramTests.cs | 11 ++
07/CamelCards.Tests/TurnTests.cs | 95 +++++++++
07/CamelCards/CamelCards.csproj | 10 +
07/CamelCards/Program.cs | 173 +++++++++++++++++
AdventOfCode2023.sln | 16 ++
9 files changed, 578 insertions(+)
create mode 100644 07/CamelCards.Tests/CamelCards.Tests.csproj
create mode 100644 07/CamelCards.Tests/CardTests.cs
create mode 100644 07/CamelCards.Tests/GlobalUsings.cs
create mode 100644 07/CamelCards.Tests/HandTests.cs
create mode 100644 07/CamelCards.Tests/ProgramTests.cs
create mode 100644 07/CamelCards.Tests/TurnTests.cs
create mode 100644 07/CamelCards/CamelCards.csproj
create mode 100644 07/CamelCards/Program.cs
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..28bb3d0
--- /dev/null
+++ b/07/CamelCards.Tests/CardTests.cs
@@ -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();
+ }
+}
\ 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..7fef4b0
--- /dev/null
+++ b/07/CamelCards.Tests/GlobalUsings.cs
@@ -0,0 +1,2 @@
+global using Xunit;
+global using FluentAssertions;
\ No newline at end of file
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