-
-
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
6 changed files
with
98 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## --- 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 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). |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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); | ||
return updates | ||
.Where(pages => Sorted(expected, pages)) | ||
.Sum(GetMiddlePage); | ||
} | ||
|
||
public object PartTwo(string input) { | ||
var (expected, updates) = Parse(input); | ||
return updates | ||
.Where(pages => !Sorted(expected, pages)) | ||
.Select(pages => Sort(expected, pages)) | ||
.Sum(GetMiddlePage); | ||
} | ||
|
||
(HashSet<string> expected, string[][] updates) Parse(string input) { | ||
var parts = input.Split("\n\n"); | ||
var expected = new HashSet<string>(parts[0].Split("\n")); | ||
var updates = parts[1].Split("\n").Select(line => line.Split(",")).ToArray(); | ||
return (expected, updates); | ||
} | ||
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); | ||
} | ||
|
||
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; | ||
} | ||
} |
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
4790 | ||
6319 |
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
Oops, something went wrong.