docs: add xml comments

This commit is contained in:
Stevan Freeborn
2023-12-04 13:29:25 -06:00
parent 475a9def07
commit 8b4b0dd8fe
3 changed files with 120 additions and 19 deletions
+34 -9
View File
@@ -12,24 +12,49 @@ public class PuzzleSolverTests
result.Should().Be(expected);
}
[Theory]
[MemberData(nameof(TestData.GetTotalCardCountData), MemberType = typeof(TestData))]
public void GetTotalCardCount_GivenCards_ItShouldReturnExpectedValue(string[] cards, int expected)
{
var result = _sut.GetTotalCardCount(cards);
result.Should().Be(expected);
}
public static class TestData
{
private static readonly string[] ExampleInput =
[
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53",
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19",
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1",
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83",
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36",
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11",
];
private static string[] Input => File.ReadAllLines("INPUT.txt");
public static IEnumerable<object[]> GetTotalCardCountData =>
new List<object[]>
{
new object[]
{
ExampleInput,
30,
},
new object[]
{
Input,
5920640,
},
};
public static IEnumerable<object[]> SumCardValuesData =>
new List<object[]>
{
new object[]
{
new[]
{
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53",
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19",
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1",
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83",
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36",
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11",
},
ExampleInput,
13,
},
new object[]
+85 -9
View File
@@ -19,40 +19,116 @@ public class Program
var puzzleSolver = new PuzzleSolver();
var input = await File.ReadAllLinesAsync(args[0]);
var result = puzzleSolver.SumCardValues(input);
var result = args.Length is 2 && args[1] == "part2"
? puzzleSolver.GetTotalCardCount(input)
: puzzleSolver.SumCardValues(input);
Console.WriteLine($"The sum of all card values is {result}.");
if (args.Length is 2 && args[1] == "part2")
{
Console.WriteLine($"The total number of cards is {result}.");
}
else
{
Console.WriteLine($"The total value of the cards is {result}.");
}
return result;
}
}
/// <summary>
/// Solves the puzzle.
/// </summary>
public class PuzzleSolver
{
/// <summary>
/// Sums the value of the cards.
/// </summary>
/// <param name="cards">The cards.</param>
/// <returns>The sum of the cards.</returns>
public int SumCardValues(string[] cards) => cards
.Select(Card.Parse)
.Sum(c => c.GetCardValue());
/// <summary>
/// Gets the total number of cards.
/// </summary>
/// <param name="cards">The cards.</param>
/// <returns>The total number of cards.</returns>
public int GetTotalCardCount(string[] cards)
{
var cardStringsToProcess = new List<string>(cards);
for (var i = 0; i < cardStringsToProcess.Count; i++)
{
var currentCardString = cardStringsToProcess[i];
var currentCardStringIndex = Array.IndexOf(cards, currentCardString);
var currentCard = Card.Parse(currentCardString);
var numOfMatchingNumbersForCurrentCard = currentCard.MatchingNumbers.Count;
var cardIndexesToCopy = Enumerable.Range(
currentCardStringIndex + 1,
Math.Min(
numOfMatchingNumbersForCurrentCard,
cards.Length - currentCardStringIndex - 1
)
);
foreach (var index in cardIndexesToCopy)
{
cardStringsToProcess.Add(cards[index]);
}
}
return cardStringsToProcess.Count;
}
}
/// <summary>
/// Represents a card.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="winningNumbers">The winning numbers.</param>
/// <param name="numbers">The numbers.</param>
/// <returns>An instance of <see cref="Card"/>.</returns>
public class Card(
int id,
List<int> winningNumbers,
List<int> numbers
)
{
/// <summary>
/// Gets the identifier.
/// </summary>
public int Id { get; init; } = id;
/// <summary>
/// Gets the winning numbers.
/// </summary>
public List<int> WinningNumbers { get; init; } = winningNumbers;
/// <summary>
/// Gets the numbers.
/// </summary>
public List<int> Numbers { get; init; } = numbers;
public int GetCardValue()
{
var commonNumbers = WinningNumbers.Intersect(Numbers).ToList();
/// <summary>
/// Gets the matching numbers.
/// </summary>
public List<int> MatchingNumbers => Numbers.Intersect(WinningNumbers).ToList();
return commonNumbers.Count is 1
? 1
: (int)Math.Pow(2, commonNumbers.Count - 1);
}
/// <summary>
/// Gets the value of the card.
/// </summary>
/// <returns>The value of the card.</returns>
public int GetCardValue() => MatchingNumbers.Count is 1
? 1
: (int)Math.Pow(2, MatchingNumbers.Count - 1);
/// <summary>
/// Parses the specified input.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>An instance of <see cref="Card"/>.</returns>
public static Card Parse(string input)
{
var parts = input.Split(':');