docs: add xml comments

This commit is contained in:
Stevan Freeborn
2023-12-06 14:42:07 -06:00
parent a28beda200
commit 79726bc978
4 changed files with 115 additions and 6 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
global using Xunit;
global using FluentAssertions;
global using Xunit;
+111 -3
View File
@@ -32,14 +32,32 @@ public class Program
}
}
/// <summary>
/// Represents an almanac.
/// </summary>
/// <param name="seeds">The seeds.</param>
/// <param name="seedRanges">The seed ranges.</param>
/// <param name="maps">The maps.</param>
/// <returns>An instance of <see cref="Almanac"/>.</returns>
public class Almanac(
List<long> seeds,
List<SeedRange> seedRanges,
List<Map> maps
)
{
/// <summary>
/// Gets the seed ranges.
/// </summary>
public List<SeedRange> SeedRanges { get; init; } = seedRanges;
/// <summary>
/// Gets the seeds.
/// </summary>
public List<long> Seeds { get; init; } = seeds;
/// <summary>
/// Gets the maps.
/// </summary>
public List<Map> Maps { get; init; } = maps;
private static List<long> GetSeedsFromString(string seedsString) =>
@@ -51,16 +69,26 @@ public class Almanac(
.Select(long.Parse)
.ToList();
/// <summary>
/// Gets the seed location.
/// </summary>
/// <param name="seed">The seed.</param>
/// <returns>The seed location.</returns>
public long GetSeedLocation(long seed) => Maps.Aggregate(
seed,
(currentSeed, map) => map.ConvertSourceToDestination(currentSeed)
);
public class LocationResult
private class LocationResult
{
public long Value { get; set; } = long.MaxValue;
}
/// <summary>
/// Gets the lowest seed location.
/// </summary>
/// <param name="seedsAreRanges">If set to true seeds are treated as ranges</param>
/// <returns>The lowest seed location.</returns>
public long GetLowestSeedLocation(bool seedsAreRanges = false)
{
var lowestLocation = new LocationResult();
@@ -101,11 +129,14 @@ public class Almanac(
}
}
File.WriteAllText("output.txt", lowestLocation.Value.ToString());
return lowestLocation.Value;
}
/// <summary>
/// Parses the seeds as ranges.
/// </summary>
/// <param name="seedsString">The seeds string.</param>
/// <returns>The seeds as ranges.</returns>
public static List<SeedRange> ParseSeedsAsRanges(string seedsString)
{
var seeds = new List<SeedRange>();
@@ -122,6 +153,11 @@ public class Almanac(
return seeds;
}
/// <summary>
/// Parses the specified almanac.
/// </summary>
/// <param name="almanac">The almanac.</param>
/// <returns>An instance of <see cref="Almanac"/>.</returns>
public static Almanac Parse(string[] almanac)
{
var seedsString = almanac[0].Split(':')[1];
@@ -166,22 +202,45 @@ public class Almanac(
}
}
/// <summary>
/// Represents a map.
/// </summary>
/// <param name="ranges">The ranges contained within the map</param>
/// <returns>An instance of <see cref="Map"/>.</returns>
public class Map(
List<Range> ranges
)
{
/// <summary>
/// Gets the ranges.
/// </summary>
public List<Range> Ranges { get; init; } = [.. ranges.OrderBy(range => range.SourceStart)];
/// <summary>
/// Parses a map from the specified ranges.
/// </summary>
/// <param name="ranges">The ranges.</param>
/// <returns>An instance of <see cref="Map"/>.</returns>
public static Map Parse(string[] ranges) =>
new(
ranges.Select(Range.Parse).ToList()
);
/// <summary>
/// Gets the source range.
/// </summary>
/// <param name="sourceValue">The source value.</param>
/// <returns>The source <see cref="Range"/>.</returns>
public Range? GetSourceRange(long sourceValue) =>
Ranges
.Where(range => range.SourceStart <= sourceValue && range.SourceEnd >= sourceValue)
.FirstOrDefault();
/// <summary>
/// Converts the source to destination.
/// </summary>
/// <param name="valueToConvert">The value to convert.</param>
/// <returns>The converted value.</returns>
public long ConvertSourceToDestination(long valueToConvert)
{
var sourceRange = GetSourceRange(valueToConvert);
@@ -199,28 +258,77 @@ public class Map(
}
}
/// <summary>
/// Represents a seed range.
/// </summary>
/// <param name="start">The start.</param>
/// <param name="length">The length.</param>
/// <returns>An instance of <see cref="SeedRange"/>.</returns>
public class SeedRange(
long start,
long length
)
{
/// <summary>
/// Gets the start of the range. It is inclusive.
/// </summary>
public long Start { get; init; } = start;
/// <summary>
/// Gets the length of the range.
/// </summary>
public long Length { get; init; } = length;
/// <summary>
/// Gets the end of the range. It is inclusive.
/// </summary>
public long End => Start + Length - 1;
}
/// <summary>
/// Represents a map range.
/// </summary>
/// <param name="rangeLength">Length of the range.</param>
/// <param name="sourceStart">The source start.</param>
/// <param name="destinationStart">The destination start.</param>
/// <returns>An instance of <see cref="Range"/>.</returns>
public class Range(
long rangeLength,
long sourceStart,
long destinationStart
)
{
/// <summary>
/// Gets the length of the range.
/// </summary>
public long RangeLength { get; init; } = rangeLength;
/// <summary>
/// Gets the start of the source range. It is inclusive.
/// </summary>
public long SourceStart { get; init; } = sourceStart;
/// <summary>
/// Gets the end of the source range. It is inclusive.
/// </summary>
public long SourceEnd => SourceStart + RangeLength - 1;
/// <summary>
/// Gets the start of the destination range. It is inclusive.
/// </summary>
public long DestinationStart { get; init; } = destinationStart;
/// <summary>
/// Gets the end of the destination range. It is inclusive.
/// </summary>
public long DestinationEnd => DestinationStart + RangeLength - 1;
/// <summary>
/// Parses the specified range.
/// </summary>
/// <param name="range">The range.</param>
/// <returns>An instance of <see cref="Range"/>.</returns>
/// <exception cref="ArgumentException">Range must be in the format of 'DestinationRangeStart:long SourceRangeStart:long RangeLength:long'</exception>
public static Range Parse(string range)
{
var parts = range.Split(
+1 -1
View File
@@ -41,7 +41,7 @@ dotnet run -- <path-to-input-file> part2
| 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/) | |
| 05 | [Problem](./05/PROBLEM.md) | [Solution](./05/) | ✅ | I brute forced part 2 using parallelism. I know shame. |
| 06 | [Problem](./06/PROBLEM.md) | [Solution](./06/) | ⌛ |
| 07 | [Problem](./07/PROBLEM.md) | [Solution](./07/) | ⌛ |
| 08 | [Problem](./08/PROBLEM.md) | [Solution](./08/) | ⌛ |