From 79726bc978d3aabecaf4421afe46c650cf2721d1 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:42:07 -0600 Subject: [PATCH] docs: add xml comments --- 04/Scratchcards.Tests/GlobalUsings.cs | 2 +- 05/IYGASAF.Tests/GlobalUsings.cs | 3 +- 05/IYGASAF/Program.cs | 114 +++++++++++++++++++++++++- README.md | 2 +- 4 files changed, 115 insertions(+), 6 deletions(-) diff --git a/04/Scratchcards.Tests/GlobalUsings.cs b/04/Scratchcards.Tests/GlobalUsings.cs index 2d5b473..d6f0167 100644 --- a/04/Scratchcards.Tests/GlobalUsings.cs +++ b/04/Scratchcards.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/05/IYGASAF.Tests/GlobalUsings.cs b/05/IYGASAF.Tests/GlobalUsings.cs index 7fef4b0..2d5b473 100644 --- a/05/IYGASAF.Tests/GlobalUsings.cs +++ b/05/IYGASAF.Tests/GlobalUsings.cs @@ -1,2 +1,3 @@ +global using FluentAssertions; + global using Xunit; -global using FluentAssertions; \ No newline at end of file diff --git a/05/IYGASAF/Program.cs b/05/IYGASAF/Program.cs index e6691ab..de90804 100644 --- a/05/IYGASAF/Program.cs +++ b/05/IYGASAF/Program.cs @@ -32,14 +32,32 @@ public class Program } } +/// +/// Represents an almanac. +/// +/// The seeds. +/// The seed ranges. +/// The maps. +/// An instance of . public class Almanac( List seeds, List seedRanges, List maps ) { + /// + /// Gets the seed ranges. + /// public List SeedRanges { get; init; } = seedRanges; + + /// + /// Gets the seeds. + /// public List Seeds { get; init; } = seeds; + + /// + /// Gets the maps. + /// public List Maps { get; init; } = maps; private static List GetSeedsFromString(string seedsString) => @@ -51,16 +69,26 @@ public class Almanac( .Select(long.Parse) .ToList(); + /// + /// Gets the seed location. + /// + /// The seed. + /// The seed location. 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; } + /// + /// Gets the lowest seed location. + /// + /// If set to true seeds are treated as ranges + /// The lowest seed location. 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; } + /// + /// Parses the seeds as ranges. + /// + /// The seeds string. + /// The seeds as ranges. public static List ParseSeedsAsRanges(string seedsString) { var seeds = new List(); @@ -122,6 +153,11 @@ public class Almanac( return seeds; } + /// + /// Parses the specified almanac. + /// + /// The almanac. + /// An instance of . public static Almanac Parse(string[] almanac) { var seedsString = almanac[0].Split(':')[1]; @@ -166,22 +202,45 @@ public class Almanac( } } +/// +/// Represents a map. +/// +/// The ranges contained within the map +/// An instance of . public class Map( List ranges ) { + /// + /// Gets the ranges. + /// public List Ranges { get; init; } = [.. ranges.OrderBy(range => range.SourceStart)]; + /// + /// Parses a map from the specified ranges. + /// + /// The ranges. + /// An instance of . public static Map Parse(string[] ranges) => new( ranges.Select(Range.Parse).ToList() ); + /// + /// Gets the source range. + /// + /// The source value. + /// The source . public Range? GetSourceRange(long sourceValue) => Ranges .Where(range => range.SourceStart <= sourceValue && range.SourceEnd >= sourceValue) .FirstOrDefault(); + /// + /// Converts the source to destination. + /// + /// The value to convert. + /// The converted value. public long ConvertSourceToDestination(long valueToConvert) { var sourceRange = GetSourceRange(valueToConvert); @@ -199,28 +258,77 @@ public class Map( } } +/// +/// Represents a seed range. +/// +/// The start. +/// The length. +/// An instance of . public class SeedRange( long start, long length ) { + /// + /// Gets the start of the range. It is inclusive. + /// public long Start { get; init; } = start; + + /// + /// Gets the length of the range. + /// public long Length { get; init; } = length; + + /// + /// Gets the end of the range. It is inclusive. + /// public long End => Start + Length - 1; } +/// +/// Represents a map range. +/// +/// Length of the range. +/// The source start. +/// The destination start. +/// An instance of . public class Range( long rangeLength, long sourceStart, long destinationStart ) { + /// + /// Gets the length of the range. + /// public long RangeLength { get; init; } = rangeLength; + + /// + /// Gets the start of the source range. It is inclusive. + /// public long SourceStart { get; init; } = sourceStart; + + /// + /// Gets the end of the source range. It is inclusive. + /// public long SourceEnd => SourceStart + RangeLength - 1; + + /// + /// Gets the start of the destination range. It is inclusive. + /// public long DestinationStart { get; init; } = destinationStart; + + /// + /// Gets the end of the destination range. It is inclusive. + /// public long DestinationEnd => DestinationStart + RangeLength - 1; + /// + /// Parses the specified range. + /// + /// The range. + /// An instance of . + /// Range must be in the format of 'DestinationRangeStart:long SourceRangeStart:long RangeLength:long' public static Range Parse(string range) { var parts = range.Split( diff --git a/README.md b/README.md index ce7b631..3d3486a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ dotnet run -- 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/) | ⌛ |