-
-
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
7 changed files
with
133 additions
and
43 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,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. |
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,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(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
1368 | ||
dd,ig,il,im,kb,kr,pe,ti,tv,vr,we,xu,zi |
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.