Skip to content

Commit

Permalink
2024/05
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 5, 2024
1 parent 592ba61 commit f4342d4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 24 deletions.
8 changes: 8 additions & 0 deletions 2024/Day05/README.md
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).
50 changes: 50 additions & 0 deletions 2024/Day05/Solution.cs
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 added 2024/Day05/input.in
Binary file not shown.
2 changes: 2 additions & 0 deletions 2024/Day05/input.refout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4790
6319
52 changes: 33 additions & 19 deletions 2024/SplashScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public void Show() {

var color = Console.ForegroundColor;
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ $year = 2024\n ");
Write(0xcc00, false, "\n ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ /* 2024 */\n \n ");
Write(0xcc00, false, " ");
Write(0xcccccc, false, ".--'");
Write(0xe3b585, false, "~ ~ ~");
Write(0xcccccc, false, "| .-' ");
Expand Down Expand Up @@ -48,9 +48,9 @@ public void Show() {
Write(0xcccccc, false, "| 3 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x1461f, false, "@");
Write(0x427322, false, "@");
Write(0x5eabb4, false, "..");
Write(0x7fbd39, false, "@");
Write(0x488813, false, "@");
Write(0xe3b585, false, "'. ~ ");
Write(0xcc00, false, "\" ' ");
Write(0xe3b585, false, "~ ");
Expand All @@ -66,21 +66,35 @@ public void Show() {
Write(0xcc00, false, ".'");
Write(0xcccccc, false, "| 4 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, "| | | .. | ");
Write(0x666666, false, " 5\n 6\n ");
Write(0x666666, false, " 7\n 8\n ");
Write(0x666666, false, " 9\n ");
Write(0x666666, false, " 10\n 11\n ");
Write(0x666666, false, " 12\n ");
Write(0x666666, false, " 13\n 14\n ");
Write(0x666666, false, " 15\n ");
Write(0x666666, false, " 16\n 17\n ");
Write(0x666666, false, " 18\n ");
Write(0x666666, false, " 19\n 20\n ");
Write(0x666666, false, " 21\n ");
Write(0x666666, false, " 22\n 23\n ");
Write(0x666666, false, " 24\n ");
Write(0x666666, false, "25\n \n");
Write(0xcccccc, false, "|");
Write(0x4d8b03, false, "_");
Write(0x5eabb4, false, ".~.");
Write(0x1461f, false, "_");
Write(0x7fbd39, false, "#");
Write(0xe3b585, false, "'.. ~ ~ ");
Write(0xffff66, true, "*");
Write(0xcccccc, false, "| | ");
Write(0xaaaaaa, false, "_| |_ ");
Write(0xcccccc, false, "..\\_");
Write(0x886655, false, "\\_ ");
Write(0xcc00, false, "..'");
Write(0xffff66, true, "* ");
Write(0xcccccc, false, "| 5 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, "| | | .' '. | ");
Write(0x666666, false, " 6\n 7\n ");
Write(0x666666, false, " 8\n 9\n ");
Write(0x666666, false, " 10\n ");
Write(0x666666, false, " 11\n 12\n ");
Write(0x666666, false, " 13\n ");
Write(0x666666, false, " 14\n 15\n ");
Write(0x666666, false, " 16\n ");
Write(0x666666, false, " 17\n 18\n ");
Write(0x666666, false, " 19\n ");
Write(0x666666, false, " 20\n 21\n ");
Write(0x666666, false, " 22\n ");
Write(0x666666, false, " 23\n 24\n ");
Write(0x666666, false, " 25\n \n");

Console.ForegroundColor = color;
Console.WriteLine();
Expand Down
Loading

0 comments on commit f4342d4

Please sign in to comment.