-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
17 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
## --- Day 5: Print Queue --- | ||
Satisfied with their search on Ceres, the squadron of scholars suggests subsequently scanning the stationery stacks of sub-basement 17. | ||
|
||
The North Pole printing department is busier than ever this close to Christmas, and while The Historians continue their search of this historically significant facility, an Elf operating a [very familiar printer](/2017/day/1) beckons you over. | ||
The North Pole printing department is busier than ever this close to Christmas, and while The Historians continue their search of this historically significant facility, an Elf operating a **very familiar printer** beckons you over. | ||
|
||
The Elf must recognize you, because they waste no time explaining that the new <em>sleigh launch safety manual</em> updates won't print correctly. Failure to update the safety manuals would be dire indeed, so you offer your services. | ||
|
||
Read the [full puzzle](https://adventofcode.com/2024/day/5). | ||
|
||
The constraints in both my input and the provided sample input define a total ordering of the pages, which I leveraged in my solution. I implemented a custom parser that returns the list of updates to be printed and a page comparison function. That's all we need. In `Part1`, we check which updates are in the correct order, while in `Part2`, we handle the remaining updates by applying .NET's built-in `OrderBy` function with our custom comparer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,39 @@ | ||
namespace AdventOfCode.Y2024.Day05; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
[ProblemName("Print Queue")] | ||
class Solution : Solver { | ||
|
||
public object PartOne(string input) { | ||
var (expected, updates) = Parse(input); | ||
var (updates, comparer) = Parse(input); | ||
return updates | ||
.Where(pages => Sorted(expected, pages)) | ||
.Where(pages => Sorted(pages, comparer)) | ||
.Sum(GetMiddlePage); | ||
} | ||
|
||
public object PartTwo(string input) { | ||
var (expected, updates) = Parse(input); | ||
var (updates, comparer) = Parse(input); | ||
return updates | ||
.Where(pages => !Sorted(expected, pages)) | ||
.Select(pages => Sort(expected, pages)) | ||
.Where(pages => !Sorted(pages, comparer)) | ||
.Select(pages => pages.OrderBy(p => p, comparer).ToArray()) | ||
.Sum(GetMiddlePage); | ||
} | ||
|
||
(HashSet<string> expected, string[][] updates) Parse(string input) { | ||
(string[][] updates, Comparer<string>) Parse(string input) { | ||
var parts = input.Split("\n\n"); | ||
var expected = new HashSet<string>(parts[0].Split("\n")); | ||
|
||
var ordering = new HashSet<string>(parts[0].Split("\n")); | ||
var comparer = | ||
Comparer<string>.Create((p1, p2) => ordering.Contains(p1 + "|" + p2) ? -1 : 1); | ||
|
||
var updates = parts[1].Split("\n").Select(line => line.Split(",")).ToArray(); | ||
return (expected, updates); | ||
return (updates, comparer); | ||
} | ||
int GetMiddlePage(string[] nums) => int.Parse(nums[nums.Length / 2]); | ||
|
||
// checks that all possible pairs in pages are in the right order | ||
bool Sorted(HashSet<string> expected, string[] pages) { | ||
var actuals = ( | ||
from i in Enumerable.Range(0, pages.Length - 1) | ||
from j in Enumerable.Range(i + 1, pages.Length - i - 1) | ||
select pages[i] + "|" + pages[j] | ||
); | ||
return actuals.All(expected.Contains); | ||
} | ||
bool Sorted(string[] pages, Comparer<string> comparer) => | ||
Enumerable.SequenceEqual(pages, pages.OrderBy(x=>x, comparer)); | ||
|
||
string[] Sort(HashSet<string> expected, string[] pages) { | ||
// Ideally we would do some topological sorting, but it's an overkill for today. | ||
// A single call to Array.Sort solves my input, so that' how life is... I might | ||
// get back to this later but probably not :D | ||
Array.Sort(pages, (page1, page2) => expected.Contains(page1 + "|" + page2) ? -1 : 1); | ||
return pages; | ||
} | ||
} |