feat: solved day 5 part 2

This commit is contained in:
Stevan Freeborn
2024-12-05 23:47:44 -06:00
parent a9f9dec7aa
commit e359a73266
5 changed files with 85 additions and 18 deletions
+18
View File
@@ -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);
}
}
@@ -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]));
}
}
+10 -4
View File
@@ -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)");
+33 -9
View File
@@ -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<int>? 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;
}
}
+6 -5
View File
@@ -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! |