Skip to content

Commit

Permalink
2024/23
Browse files Browse the repository at this point in the history
  • Loading branch information
encse committed Dec 23, 2024
1 parent 97f4fe9 commit 0e9dee7
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 43 deletions.
10 changes: 10 additions & 0 deletions 2024/Day23/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## --- Day 23: LAN Party ---
As The Historians wander around a secure area at Easter Bunny HQ, you come across posters for a [LAN party](https://en.wikipedia.org/wiki/LAN_party) scheduled for today! Maybe you can find it; you connect to a nearby _datalink port_ and download a map of the local network (your puzzle input).

The network map provides a list of every <em>connection between two computers</em>. For example:

_Visit the website for the full story and [full puzzle](https://adventofcode.com/2024/day/23) description._

We tackled a graph algorithm problem today, where we had to find maximal cliques in an undirected graph. The literature provides [efficient algorithms](https://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm) for this problem, but our graph is not too large, so we can use a straightforward "poor man's" strategy as well.

I started by creating _seed_ components containing nodes that start with '_t_'. Then, I proceeded to _grow_ these components by adding a single node to them in all possible ways. First, I identify the neighbors of each _t_ node, followed by the triples which is required to solve Part 1. In Part 2, this process is continued until a single maximal component remains.
58 changes: 58 additions & 0 deletions 2024/Day23/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace AdventOfCode.Y2024.Day23;

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Graph = System.Collections.Generic.Dictionary<string, System.Collections.Generic.HashSet<string>>;
using Component = string;

[ProblemName("LAN Party")]
class Solution : Solver {
public object PartOne(string input) {
var g = GetGraph(input);
var components = GetSeed(g);
components = Grow(g, components);
components = Grow(g, components);
return components.Count;
}

public object PartTwo(string input) {
var g = GetGraph(input);
var components = GetSeed(g);
while (components.Count > 1) {
components = Grow(g, components);
}
return components.Single();
}

HashSet<Component> GetSeed(Graph g) => g.Keys.Where(k=>k.StartsWith("t")).ToHashSet();

HashSet<Component> Grow(Graph g, HashSet<Component> components) => (
from c in components
let members = Members(c)
from neighbour in members.SelectMany(m => g[m]).Distinct()
where !members.Contains(neighbour)
where members.All(m => g[neighbour].Contains(m))
select Extend(c, neighbour)
).ToHashSet();

IEnumerable<string> Members(Component c) =>
c.Split(",");
Component Extend(Component c, string item) =>
string.Join(",", Members(c).Append(item).OrderBy(x=>x));

Graph GetGraph(string input) {
var edges =
from line in input.Split("\n")
let nodes = line.Split("-")
from edge in new []{(nodes[0], nodes[1]), (nodes[1], nodes[0])}
select (From: edge.Item1, To: edge.Item2);

return (
from e in edges
group e by e.From into g
select (g.Key, g.Select(e => e.To).ToHashSet())
).ToDictionary();
}
}
Binary file added 2024/Day23/illustration.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2024/Day23/input.in
Binary file not shown.
2 changes: 2 additions & 0 deletions 2024/Day23/input.refout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1368
dd,ig,il,im,kb,kr,pe,ti,tv,vr,we,xu,zi
78 changes: 49 additions & 29 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 █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ 0x0000 | 2024\n ");
Write(0xcc00, false, " \n ");
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ $year = 2024\n ");
Write(0xcc00, false, "\n ");
Write(0x888888, false, " .-----. .------------------. \n ");
Write(0xcccccc, false, ".--'");
Write(0xe3b585, false, "~ ~ ~");
Expand Down Expand Up @@ -49,9 +49,9 @@ public void Show() {
Write(0xcccccc, false, "| 3 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x488813, false, "@");
Write(0x427322, false, "@");
Write(0x5eabb4, false, "..");
Write(0x4d8b03, false, "@");
Write(0x1461f, false, "@");
Write(0xe3b585, false, "'. ~ ");
Write(0xcc00, false, "\" ' ");
Write(0xe3b585, false, "~ ");
Expand All @@ -68,10 +68,9 @@ public void Show() {
Write(0xcccccc, false, "| 4 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x4d8b03, false, "_");
Write(0x427322, false, "_");
Write(0x5eabb4, false, ".~.");
Write(0x4d8b03, false, "_");
Write(0x427322, false, "@");
Write(0x4d8b03, false, "_@");
Write(0xe3b585, false, "'.. ~ ~ ");
Write(0xffff66, true, "*");
Write(0xcccccc, false, "| | ");
Expand All @@ -84,9 +83,10 @@ public void Show() {
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Write(0xffffff, false, "||| ");
Write(0x1461f, false, "#");
Write(0x1461f, false, "@");
Write(0x427322, false, "@");
Write(0x488813, false, "@ ");
Write(0x7fbd39, false, "@");
Write(0x4d8b03, false, "@");
Write(0xe3b585, false, "'''...");
Write(0xcccccc, false, "| |");
Write(0xa25151, false, "... ");
Expand All @@ -97,14 +97,13 @@ public void Show() {
Write(0xcccccc, false, "| 6 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0x488813, false, "#");
Write(0x427322, false, "#");
Write(0xffffff, false, "~~~");
Write(0x488813, false, "#");
Write(0x427322, false, "@");
Write(0x7fbd39, false, "@");
Write(0x4d8b03, false, "@#");
Write(0x488813, false, "@#@");
Write(0x1461f, false, "@");
Write(0x427322, false, "@ ");
Write(0x4d8b03, false, "# ");
Write(0x7fbd39, false, "@");
Write(0x488813, false, "@ ");
Write(0xcccccc, false, "| |");
Write(0xa5a8af, false, "/\\ ");
Write(0xa25151, false, "''. ");
Expand Down Expand Up @@ -160,7 +159,8 @@ public void Show() {
Write(0xa5a8af, false, "/\\ ");
Write(0xa25151, false, "..' ");
Write(0xcccccc, false, "| | ");
Write(0xffffff, false, ". ");
Write(0xffffff, false, ". ");
Write(0xb5ed, false, ". ");
Write(0xcccccc, false, "| 11 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "| ");
Expand All @@ -170,10 +170,9 @@ public void Show() {
Write(0x333333, false, "::");
Write(0xffff66, true, ":");
Write(0x333333, false, "::");
Write(0xcccccc, false, "| |");
Write(0xa2db, false, ". ");
Write(0xffffff, false, ". ");
Write(0xa2db, false, ".");
Write(0xcccccc, false, "| | ");
Write(0xffffff, false, ". ");
Write(0xa2db, false, ". . ");
Write(0xcccccc, false, "| 12 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Expand All @@ -190,8 +189,8 @@ public void Show() {
Write(0xcc00, false, "...");
Write(0xffffff, false, "'..''");
Write(0xcccccc, false, "| |");
Write(0xffffff, true, ". ");
Write(0x333333, false, ":");
Write(0xffffff, true, ". ");
Write(0x333333, false, ".:");
Write(0x9900, true, ":::");
Write(0x333333, false, ":");
Write(0xcccccc, false, "| |");
Expand Down Expand Up @@ -232,8 +231,8 @@ public void Show() {
Write(0x5555bb, false, "~ ");
Write(0xcc00, false, ":");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " '. ");
Write(0x333333, false, ". ");
Write(0x666666, false, " '. ");
Write(0x333333, false, ". ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "┬o┤ten├─");
Write(0xcccccc, false, "| 17 ");
Expand All @@ -242,8 +241,7 @@ public void Show() {
Write(0xcc00, false, "'..' .'");
Write(0xcccccc, false, "| |");
Write(0x666666, false, " '");
Write(0x456efe, true, "o ");
Write(0x333333, false, ". ");
Write(0x456efe, true, "o ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "┘");
Write(0xffff66, true, "*");
Expand All @@ -254,7 +252,9 @@ public void Show() {
Write(0x5555bb, false, "~ ");
Write(0xcc00, false, "..' ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, ": '. ");
Write(0x666666, false, ":");
Write(0x333333, false, ".");
Write(0x666666, false, " '. ");
Write(0xcccccc, false, "| |");
Write(0x666666, false, "─┘├┬┬┬┴─");
Write(0xcccccc, false, "| 19 ");
Expand Down Expand Up @@ -307,9 +307,29 @@ public void Show() {
Write(0x66ff, true, "O");
Write(0xcccccc, false, "| 22 ");
Write(0xffff66, false, "**\n ");
Write(0xcccccc, false, "|");
Write(0xff0000, false, "/ ");
Write(0x880000, false, "/ ");
Write(0xff0000, false, "/\\|");
Write(0x66ff, false, "| | )");
Write(0xaaaaaa, false, "/");
Write(0xe6410b, false, "~");
Write(0xaaaaaa, false, "\\ ");
Write(0xcccccc, false, "| |");
Write(0x66ff, false, "___");
Write(0xff0000, false, "|");
Write(0xcccccc, false, "\\|");
Write(0x66ff, false, "________");
Write(0x9900, false, ">");
Write(0x66ff, true, "O");
Write(0x9900, false, ">>");
Write(0xff9900, true, "o");
Write(0x9900, false, ">>");
Write(0xff9900, true, "o");
Write(0xcccccc, false, "| 23 ");
Write(0xffff66, false, "**\n ");
Write(0x333333, false, "| | | | ");
Write(0x666666, false, "23\n 24\n ");
Write(0x666666, false, " 25\n \n");
Write(0x666666, false, "24\n 25\n \n");

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

0 comments on commit 0e9dee7

Please sign in to comment.