feat: solve part 1 and part 2 of puzzle 6

This commit is contained in:
Stevan Freeborn
2023-12-06 20:21:11 -06:00
parent 443e7385fd
commit 6462fd8027
6 changed files with 201 additions and 12 deletions
+16
View File
@@ -0,0 +1,16 @@
namespace WaitForIt.Tests;
public class ProgramTests
{
public void Main_WhenGivenInput_ItShouldReturnTheProductOfTheMarginOfErrors()
{
var result = Program.Main(["INPUT.txt"]);
result.Should().Be(1710720);
}
public void Main_WhenGivenInputAndPart2_ItShouldReturnTheNumberOfWaysToWin()
{
var result = Program.Main(["INPUT.txt", "part2"]);
result.Should().Be(35349468);
}
}
+38
View File
@@ -0,0 +1,38 @@
namespace WaitForIt.Tests;
public class PuzzleParserTests
{
private readonly PuzzleParser _sut = new();
[Theory]
[MemberData(nameof(TestData.ValidInputData), MemberType = typeof(TestData))]
public void Parse_WhenGivenValidInput_ItShouldReturnAListOfRaces(string[] input, List<Race> expected)
{
var result = _sut.ParseRaces(input);
result.Should().BeEquivalentTo(expected);
}
public static class TestData
{
private static readonly string[] ValidInput =
[
"Time: 7 15 30",
"Distance: 9 40 200"
];
public static IEnumerable<object[]> ValidInputData =>
new List<object[]>
{
new object[]
{
ValidInput,
new List<Race>
{
new(7, 9),
new(15, 40),
new(30, 200)
}
}
};
}
}
+14
View File
@@ -0,0 +1,14 @@
namespace WaitForIt.Tests;
public class RaceTests
{
[Theory]
[InlineData(7, 9, 4)]
[InlineData(15, 40, 8)]
[InlineData(30, 200, 9)]
public void CalculateNumberOfWaysToWin_WhenCalled_ItShouldReturnNumberOfWaysToWin(int raceDuration, int distanceRecord, int expected)
{
var result = new Race(raceDuration, distanceRecord).CalculateNumberOfWaysToWin();
result.Should().Be(expected);
}
}
-10
View File
@@ -1,10 +0,0 @@
namespace WaitForIt.Tests;
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
@@ -27,4 +27,10 @@
<ProjectReference Include="..\WaitForIt\WaitForIt.csproj" /> <ProjectReference Include="..\WaitForIt\WaitForIt.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="..\INPUT.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project> </Project>
+127 -2
View File
@@ -1,2 +1,127 @@
// See https://aka.ms/new-console-template for more information namespace WaitForIt;
Console.WriteLine("Hello, World!");
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 parser = new PuzzleParser();
var input = await File.ReadAllLinesAsync(args[0]);
long result = args.Length > 1 && args[1] == "part2"
? parser
.ParseRace(input)
.CalculateNumberOfWaysToWin()
: parser.ParseRaces(input)
.Select(r => r.CalculateNumberOfWaysToWin())
.Aggregate((long)1, (acc, curr) => acc * curr);
Console.WriteLine($"The product of the margin of errors is {result}.");
return (int)result;
}
}
/// <summary>
/// Parses the puzzle input.
/// </summary>
public class PuzzleParser
{
private List<long> GetValues(string input) => input
.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Skip(1)
.Select(long.Parse)
.ToList();
/// <summary>
/// Parses the puzzle input to list of races
/// </summary>
/// <param name="racesInput">The list of races</param>
/// <returns>A list containing instances of <see cref="Race"/>.</returns>
public List<Race> ParseRaces(string[] racesInput)
{
var races = new List<Race>();
var durations = GetValues(racesInput[0]);
var distances = GetValues(racesInput[1]);
if (durations.Count != distances.Count)
{
throw new ArgumentException("The number of times and distances must be equal.");
}
for (int i = 0; i < durations.Count; i++)
{
races.Add(new Race(durations[i], distances[i]));
}
return races;
}
/// <summary>
/// Parses the puzzle input as single race
/// </summary>
/// <param name="racesInput">The list of races</param>
/// <returns>An instance of <see cref="Race"/>.</returns>
public Race ParseRace(string[] racesInput)
{
var duration = string.Join("", GetValues(racesInput[0]).Select(v => v.ToString()));
var distance = string.Join("", GetValues(racesInput[1]).Select(v => v.ToString()));
return new Race(long.Parse(duration), long.Parse(distance));
}
}
/// <summary>
/// Represents a Race
/// </summary>
/// <param name="duration">The duration of the race</param>
/// <param name="distanceRecord">The distance record</param>
/// <returns>An instance of <see cref="Race"/>.</returns>
public class Race(
long duration,
long distanceRecord
)
{
/// <summary>
/// Gets the duration of the race
/// </summary>
public long Duration { get; init; } = duration;
/// <summary>
/// Gets the distance record
/// </summary>
public long DistanceRecord { get; init; } = distanceRecord;
/// <summary>
/// Calculates the number of ways the race can be won.
/// </summary>
/// <returns>The number of ways the race can be won.</returns>
public long CalculateNumberOfWaysToWin()
{
var numberOfWaysToWin = 0;
for (var secsHeld = 0; secsHeld < Duration; secsHeld++)
{
var speed = 1 * secsHeld;
var distance = speed * (Duration - secsHeld);
if (distance > DistanceRecord)
{
numberOfWaysToWin++;
}
}
return numberOfWaysToWin;
}
}