Skip to content

Commit 0e9dee7

Browse files
committed
2024/23
1 parent 97f4fe9 commit 0e9dee7

File tree

7 files changed

+133
-43
lines changed

7 files changed

+133
-43
lines changed

2024/Day23/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## --- Day 23: LAN Party ---
2+
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).
3+
4+
The network map provides a list of every <em>connection between two computers</em>. For example:
5+
6+
_Visit the website for the full story and [full puzzle](https://adventofcode.com/2024/day/23) description._
7+
8+
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.
9+
10+
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.

2024/Day23/Solution.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace AdventOfCode.Y2024.Day23;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.Immutable;
6+
using System.Linq;
7+
using Graph = System.Collections.Generic.Dictionary<string, System.Collections.Generic.HashSet<string>>;
8+
using Component = string;
9+
10+
[ProblemName("LAN Party")]
11+
class Solution : Solver {
12+
public object PartOne(string input) {
13+
var g = GetGraph(input);
14+
var components = GetSeed(g);
15+
components = Grow(g, components);
16+
components = Grow(g, components);
17+
return components.Count;
18+
}
19+
20+
public object PartTwo(string input) {
21+
var g = GetGraph(input);
22+
var components = GetSeed(g);
23+
while (components.Count > 1) {
24+
components = Grow(g, components);
25+
}
26+
return components.Single();
27+
}
28+
29+
HashSet<Component> GetSeed(Graph g) => g.Keys.Where(k=>k.StartsWith("t")).ToHashSet();
30+
31+
HashSet<Component> Grow(Graph g, HashSet<Component> components) => (
32+
from c in components
33+
let members = Members(c)
34+
from neighbour in members.SelectMany(m => g[m]).Distinct()
35+
where !members.Contains(neighbour)
36+
where members.All(m => g[neighbour].Contains(m))
37+
select Extend(c, neighbour)
38+
).ToHashSet();
39+
40+
IEnumerable<string> Members(Component c) =>
41+
c.Split(",");
42+
Component Extend(Component c, string item) =>
43+
string.Join(",", Members(c).Append(item).OrderBy(x=>x));
44+
45+
Graph GetGraph(string input) {
46+
var edges =
47+
from line in input.Split("\n")
48+
let nodes = line.Split("-")
49+
from edge in new []{(nodes[0], nodes[1]), (nodes[1], nodes[0])}
50+
select (From: edge.Item1, To: edge.Item2);
51+
52+
return (
53+
from e in edges
54+
group e by e.From into g
55+
select (g.Key, g.Select(e => e.To).ToHashSet())
56+
).ToDictionary();
57+
}
58+
}

2024/Day23/illustration.jpeg

160 KB
Loading

2024/Day23/input.in

19.8 KB
Binary file not shown.

2024/Day23/input.refout

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1368
2+
dd,ig,il,im,kb,kr,pe,ti,tv,vr,we,xu,zi

2024/SplashScreen.cs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public void Show() {
88

99
var color = Console.ForegroundColor;
1010
Write(0xcc00, false, " ▄█▄ ▄▄█ ▄ ▄ ▄▄▄ ▄▄ ▄█▄ ▄▄▄ ▄█ ▄▄ ▄▄▄ ▄▄█ ▄▄▄\n █▄█ █ █ █ █ █▄█ █ █ █ █ █ █▄ ");
11-
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ 0x0000 | 2024\n ");
12-
Write(0xcc00, false, " \n ");
11+
Write(0xcc00, false, " █ █ █ █ █ █▄█\n █ █ █▄█ ▀▄▀ █▄▄ █ █ █▄ █▄█ █ █▄ █▄█ █▄█ █▄▄ $year = 2024\n ");
12+
Write(0xcc00, false, "\n ");
1313
Write(0x888888, false, " .-----. .------------------. \n ");
1414
Write(0xcccccc, false, ".--'");
1515
Write(0xe3b585, false, "~ ~ ~");
@@ -49,9 +49,9 @@ public void Show() {
4949
Write(0xcccccc, false, "| 3 ");
5050
Write(0xffff66, false, "**\n ");
5151
Write(0xcccccc, false, "|");
52-
Write(0x488813, false, "@");
52+
Write(0x427322, false, "@");
5353
Write(0x5eabb4, false, "..");
54-
Write(0x4d8b03, false, "@");
54+
Write(0x1461f, false, "@");
5555
Write(0xe3b585, false, "'. ~ ");
5656
Write(0xcc00, false, "\" ' ");
5757
Write(0xe3b585, false, "~ ");
@@ -68,10 +68,9 @@ public void Show() {
6868
Write(0xcccccc, false, "| 4 ");
6969
Write(0xffff66, false, "**\n ");
7070
Write(0xcccccc, false, "|");
71-
Write(0x4d8b03, false, "_");
71+
Write(0x427322, false, "_");
7272
Write(0x5eabb4, false, ".~.");
73-
Write(0x4d8b03, false, "_");
74-
Write(0x427322, false, "@");
73+
Write(0x4d8b03, false, "_@");
7574
Write(0xe3b585, false, "'.. ~ ~ ");
7675
Write(0xffff66, true, "*");
7776
Write(0xcccccc, false, "| | ");
@@ -84,9 +83,10 @@ public void Show() {
8483
Write(0xffff66, false, "**\n ");
8584
Write(0xcccccc, false, "| ");
8685
Write(0xffffff, false, "||| ");
87-
Write(0x1461f, false, "#");
86+
Write(0x1461f, false, "@");
8887
Write(0x427322, false, "@");
89-
Write(0x488813, false, "@ ");
88+
Write(0x7fbd39, false, "@");
89+
Write(0x4d8b03, false, "@");
9090
Write(0xe3b585, false, "'''...");
9191
Write(0xcccccc, false, "| |");
9292
Write(0xa25151, false, "... ");
@@ -97,14 +97,13 @@ public void Show() {
9797
Write(0xcccccc, false, "| 6 ");
9898
Write(0xffff66, false, "**\n ");
9999
Write(0xcccccc, false, "|");
100-
Write(0x488813, false, "#");
100+
Write(0x427322, false, "#");
101101
Write(0xffffff, false, "~~~");
102-
Write(0x488813, false, "#");
103-
Write(0x427322, false, "@");
104-
Write(0x7fbd39, false, "@");
105-
Write(0x4d8b03, false, "@#");
102+
Write(0x488813, false, "@#@");
106103
Write(0x1461f, false, "@");
107-
Write(0x427322, false, "@ ");
104+
Write(0x4d8b03, false, "# ");
105+
Write(0x7fbd39, false, "@");
106+
Write(0x488813, false, "@ ");
108107
Write(0xcccccc, false, "| |");
109108
Write(0xa5a8af, false, "/\\ ");
110109
Write(0xa25151, false, "''. ");
@@ -160,7 +159,8 @@ public void Show() {
160159
Write(0xa5a8af, false, "/\\ ");
161160
Write(0xa25151, false, "..' ");
162161
Write(0xcccccc, false, "| | ");
163-
Write(0xffffff, false, ". ");
162+
Write(0xffffff, false, ". ");
163+
Write(0xb5ed, false, ". ");
164164
Write(0xcccccc, false, "| 11 ");
165165
Write(0xffff66, false, "**\n ");
166166
Write(0xcccccc, false, "| ");
@@ -170,10 +170,9 @@ public void Show() {
170170
Write(0x333333, false, "::");
171171
Write(0xffff66, true, ":");
172172
Write(0x333333, false, "::");
173-
Write(0xcccccc, false, "| |");
174-
Write(0xa2db, false, ". ");
175-
Write(0xffffff, false, ". ");
176-
Write(0xa2db, false, ".");
173+
Write(0xcccccc, false, "| | ");
174+
Write(0xffffff, false, ". ");
175+
Write(0xa2db, false, ". . ");
177176
Write(0xcccccc, false, "| 12 ");
178177
Write(0xffff66, false, "**\n ");
179178
Write(0xcccccc, false, "|");
@@ -190,8 +189,8 @@ public void Show() {
190189
Write(0xcc00, false, "...");
191190
Write(0xffffff, false, "'..''");
192191
Write(0xcccccc, false, "| |");
193-
Write(0xffffff, true, ". ");
194-
Write(0x333333, false, ":");
192+
Write(0xffffff, true, ". ");
193+
Write(0x333333, false, ".:");
195194
Write(0x9900, true, ":::");
196195
Write(0x333333, false, ":");
197196
Write(0xcccccc, false, "| |");
@@ -232,8 +231,8 @@ public void Show() {
232231
Write(0x5555bb, false, "~ ");
233232
Write(0xcc00, false, ":");
234233
Write(0xcccccc, false, "| |");
235-
Write(0x666666, false, " '. ");
236-
Write(0x333333, false, ". ");
234+
Write(0x666666, false, " '. ");
235+
Write(0x333333, false, ". ");
237236
Write(0xcccccc, false, "| |");
238237
Write(0x666666, false, "┬o┤ten├─");
239238
Write(0xcccccc, false, "| 17 ");
@@ -242,8 +241,7 @@ public void Show() {
242241
Write(0xcc00, false, "'..' .'");
243242
Write(0xcccccc, false, "| |");
244243
Write(0x666666, false, " '");
245-
Write(0x456efe, true, "o ");
246-
Write(0x333333, false, ". ");
244+
Write(0x456efe, true, "o ");
247245
Write(0xcccccc, false, "| |");
248246
Write(0x666666, false, "┘");
249247
Write(0xffff66, true, "*");
@@ -254,7 +252,9 @@ public void Show() {
254252
Write(0x5555bb, false, "~ ");
255253
Write(0xcc00, false, "..' ");
256254
Write(0xcccccc, false, "| |");
257-
Write(0x666666, false, ": '. ");
255+
Write(0x666666, false, ":");
256+
Write(0x333333, false, ".");
257+
Write(0x666666, false, " '. ");
258258
Write(0xcccccc, false, "| |");
259259
Write(0x666666, false, "─┘├┬┬┬┴─");
260260
Write(0xcccccc, false, "| 19 ");
@@ -307,9 +307,29 @@ public void Show() {
307307
Write(0x66ff, true, "O");
308308
Write(0xcccccc, false, "| 22 ");
309309
Write(0xffff66, false, "**\n ");
310+
Write(0xcccccc, false, "|");
311+
Write(0xff0000, false, "/ ");
312+
Write(0x880000, false, "/ ");
313+
Write(0xff0000, false, "/\\|");
314+
Write(0x66ff, false, "| | )");
315+
Write(0xaaaaaa, false, "/");
316+
Write(0xe6410b, false, "~");
317+
Write(0xaaaaaa, false, "\\ ");
318+
Write(0xcccccc, false, "| |");
319+
Write(0x66ff, false, "___");
320+
Write(0xff0000, false, "|");
321+
Write(0xcccccc, false, "\\|");
322+
Write(0x66ff, false, "________");
323+
Write(0x9900, false, ">");
324+
Write(0x66ff, true, "O");
325+
Write(0x9900, false, ">>");
326+
Write(0xff9900, true, "o");
327+
Write(0x9900, false, ">>");
328+
Write(0xff9900, true, "o");
329+
Write(0xcccccc, false, "| 23 ");
330+
Write(0xffff66, false, "**\n ");
310331
Write(0x333333, false, "| | | | ");
311-
Write(0x666666, false, "23\n 24\n ");
312-
Write(0x666666, false, " 25\n \n");
332+
Write(0x666666, false, "24\n 25\n \n");
313333

314334
Console.ForegroundColor = color;
315335
Console.WriteLine();

0 commit comments

Comments
 (0)