diff --git a/05/PrintQueue.Tests/PuzzleParserTests.cs b/05/PrintQueue.Tests/PuzzleParserTests.cs index 6463f0a..550230d 100644 --- a/05/PrintQueue.Tests/PuzzleParserTests.cs +++ b/05/PrintQueue.Tests/PuzzleParserTests.cs @@ -73,4 +73,22 @@ public class PuzzleParserTests await Assert.That(result).IsEqualTo(6260); } + + [Test] + public async Task Parse_WhenGivenPuzzleInputOnPart2_ItShouldReturnExpectedResult() + { + var input = await GetPuzzleInput(); + + var parseResult = _puzzleParser.Parse(input); + + var validator = new UpdateValidator(parseResult.Rules); + + var result = parseResult.Updates + .Where(u => validator.Validate(u) is false) + .Select(u => validator.Sort(u)) + .Select(u => u.GetMiddlePage()) + .Sum(); + + await Assert.That(result).IsEqualTo(5346); + } } \ No newline at end of file diff --git a/05/PrintQueue.Tests/UpdateValidatorTests.cs b/05/PrintQueue.Tests/UpdateValidatorTests.cs index fff7d18..cafb524 100644 --- a/05/PrintQueue.Tests/UpdateValidatorTests.cs +++ b/05/PrintQueue.Tests/UpdateValidatorTests.cs @@ -81,4 +81,22 @@ public class UpdateValidatorTests await Assert.That(resultTwo).IsFalse(); await Assert.That(resultThree).IsFalse(); } + + [Test] + public async Task Sort_WhenGivenInvalidUpdate_ItShouldReturnNewUpdateSorted() + { + var updateOne = new Update([75,97,47,61,53]); + var updateTwo = new Update([61,13,29]); + var updateThree = new Update([97,13,75,29,47]); + + var validator = new UpdateValidator(_exampleRules); + + var resultOne = validator.Sort(updateOne); + var resultTwo = validator.Sort(updateTwo); + var resultThree = validator.Sort(updateThree); + + await Assert.That(resultOne).IsEquivalentTo(new Update([97,75,47,61,53])); + await Assert.That(resultTwo).IsEquivalentTo(new Update([61,29,13])); + await Assert.That(resultThree).IsEquivalentTo(new Update([97,75,47,29,13])); + } } \ No newline at end of file diff --git a/05/PrintQueue/Program.cs b/05/PrintQueue/Program.cs index a72944e..14cb2fb 100644 --- a/05/PrintQueue/Program.cs +++ b/05/PrintQueue/Program.cs @@ -26,10 +26,16 @@ var parseResult = parser.Parse(input); var validator = new UpdateValidator(parseResult.Rules); -var result = parseResult.Updates - .Where(u => validator.Validate(u)) - .Select(u => u.GetMiddlePage()) - .Sum(); +var result = isPart2 + ? parseResult.Updates + .Where(u => validator.Validate(u) is false) + .Select(u => validator.Sort(u)) + .Select(u => u.GetMiddlePage()) + .Sum() + : parseResult.Updates + .Where(u => validator.Validate(u)) + .Select(u => u.GetMiddlePage()) + .Sum(); stopwatch.Stop(); Console.WriteLine($"The sum of the page numbers is {result}. ({stopwatch.ElapsedMilliseconds}ms)"); \ No newline at end of file diff --git a/05/PrintQueue/UpdateValidator.cs b/05/PrintQueue/UpdateValidator.cs index e1f6c18..8c8351f 100644 --- a/05/PrintQueue/UpdateValidator.cs +++ b/05/PrintQueue/UpdateValidator.cs @@ -21,6 +21,30 @@ class UpdateValidator Graph[rule.X].Add(rule.Y); } } + + public Update Sort(Update update) + { + var sortedUpdate = new Update(update.Pages.ToList()); + + while (Validate(sortedUpdate) is false) + { + for (var i = 0; i < sortedUpdate.Pages.Count - 1; i++) + { + var currentPage = sortedUpdate.Pages[i]; + var nextPage = sortedUpdate.Pages[i + 1]; + + if (IsOutOfOrder(currentPage, nextPage) is false) + { + continue; + } + + sortedUpdate.Pages[i] = nextPage; + sortedUpdate.Pages[i + 1] = currentPage; + } + } + + return sortedUpdate; + } public bool Validate(Update update) { @@ -28,20 +52,20 @@ class UpdateValidator foreach (var page in update.Pages) { - foreach (var previousPage in previousPages) + if (previousPages.Any(previousPage => IsOutOfOrder(previousPage, page))) { - if ( - Graph.TryGetValue(previousPage, out HashSet? value) && - value.Contains(page) is false - ) - { - return false; - } + return false; } - + previousPages.Add(page); } return true; } + + private bool IsOutOfOrder(int previousPage, int futurePage) + { + return Graph.TryGetValue(previousPage, out var dependents) && + dependents.Contains(futurePage) is false; + } } \ No newline at end of file diff --git a/README.md b/README.md index e4d1a16..3771ae3 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,10 @@ dotnet build ## Challenges -| Day | Problem | Solution | Notes | -|-----|----------------------------|-------------------------------------|----------------------------------------------------------------------------------------------| +| Day | Problem | Solution | Notes | +|-----|----------------------------|------------------------------------|----------------------------------------------------------------------------------------------| | 01 | [Problem](./01/PROBLEM.md) | [Solution](./01/HistorianHysteria/) | A great way to start! | -| 02 | [Problem](./02/PROBLEM.md) | [Solution](./02/RedNosedReports/) | Damn their was an edge case that really bit me...direction change after first pair of levels | -| 03 | [Problem](./03/PROBLEM.md) | [Solution](./03/MullItOver/) | Thank goodness this wasn't a repeat of day 2. | -| 04 | [Problem](./04/PROBLEM.md) | [Solution](./04/CeresSearch/) | This was fun! Definitely was able to see the growth in my abilities here. | +| 02 | [Problem](./02/PROBLEM.md) | [Solution](./02/RedNosedReports/) | Damn their was an edge case that really bit me...direction change after first pair of levels | +| 03 | [Problem](./03/PROBLEM.md) | [Solution](./03/MullItOver/) | Thank goodness this wasn't a repeat of day 2. | +| 04 | [Problem](./04/PROBLEM.md) | [Solution](./04/CeresSearch/) | This was fun! Definitely was able to see the growth in my abilities here. | +| 05 | [Problem](./05/PROBLEM.md) | [Solution](./05/PrintQueue/) | A graph to the rescue! | \ No newline at end of file