From fb4084d20b78dc2ba864f428cdb34bc5d2be2453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Da=CC=81vid=20Ne=CC=81meth=20Cs?= Date: Thu, 5 Dec 2024 08:14:57 +0100 Subject: [PATCH] 2024/05 --- 2024/Day05/README.md | 4 +++- 2024/Day05/Solution.cs | 39 ++++++++++++++------------------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/2024/Day05/README.md b/2024/Day05/README.md index d13907a7..ecb38045 100644 --- a/2024/Day05/README.md +++ b/2024/Day05/README.md @@ -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 sleigh launch safety manual 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. \ No newline at end of file diff --git a/2024/Day05/Solution.cs b/2024/Day05/Solution.cs index 678de727..6307a463 100644 --- a/2024/Day05/Solution.cs +++ b/2024/Day05/Solution.cs @@ -1,6 +1,5 @@ namespace AdventOfCode.Y2024.Day05; -using System; using System.Collections.Generic; using System.Linq; @@ -8,43 +7,33 @@ namespace AdventOfCode.Y2024.Day05; 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 expected, string[][] updates) Parse(string input) { + (string[][] updates, Comparer) Parse(string input) { var parts = input.Split("\n\n"); - var expected = new HashSet(parts[0].Split("\n")); + + var ordering = new HashSet(parts[0].Split("\n")); + var comparer = + Comparer.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 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 comparer) => + Enumerable.SequenceEqual(pages, pages.OrderBy(x=>x, comparer)); - string[] Sort(HashSet 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; - } } \ No newline at end of file