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 beckons you over.
The Elf must recognize you, because they waste no time explaining that the new sleigh launch safety manual updates won't print correctly. Failure to update the safety manuals would be dire indeed, so you offer your services.
Visit the website for the full story and puzzle description.
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.
(*) others say that the ordering is not total, in fact there are loops in it. But it was not an issue for the update lines we need to sort. So the thing below works only because of the Elf magic of X-mas.
namespace AdventOfCode.Y2024.Day05;
using System.Collections.Generic;
using System.Linq;
[ProblemName("Print Queue")]
class Solution : Solver {
public object PartOne(string input) {
var (updates, comparer) = Parse(input);
return updates
.Where(pages => Sorted(pages, comparer))
.Sum(GetMiddlePage);
}
public object PartTwo(string input) {
var (updates, comparer) = Parse(input);
return updates
.Where(pages => !Sorted(pages, comparer))
.Select(pages => pages.OrderBy(p => p, comparer).ToArray())
.Sum(GetMiddlePage);
}
(string[][] updates, Comparer<string>) Parse(string input) {
var parts = input.Split("\n\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 (updates, comparer);
}
int GetMiddlePage(string[] nums) => int.Parse(nums[nums.Length / 2]);
bool Sorted(string[] pages, Comparer<string> comparer) =>
Enumerable.SequenceEqual(pages, pages.OrderBy(x=>x, comparer));
}
Please ☆ my repo if you like it!