Skip to content

Commit

Permalink
day(23): LAN Party 💻
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 23, 2024
1 parent 365f4f6 commit 3040c4e
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/AdventOfCode/Day23.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Collections.Generic;
using System.Linq;
using AdventOfCode.Utilities;

namespace AdventOfCode
{
/// <summary>
/// Solver for Day 23
/// </summary>
public class Day23
{
public int Part1(string[] input)
{
SortedDictionary<string, ICollection<string>> graph = BuildGraph(input);

int total = 0;

/* Look for triangles of interconnected nodes, i.e.
node
/ \
/ \
left -- right
*/
foreach (string node in graph.Keys)
{
foreach (string left in graph[node].Where(l => string.CompareOrdinal(node, l) < 0))
{
foreach (string right in graph[left].Where(r => string.CompareOrdinal(left, r) < 0))
{
if (!graph[node].Contains(right))
{
continue;
}

// found a triangle
if (node.StartsWith('t') || left.StartsWith('t') || right.StartsWith('t'))
{
total++;
}
}
}
}

return total;
}

public string Part2(string[] input)
{
SortedDictionary<string, ICollection<string>> graph = new();

foreach (string line in input)
{
string[] elements = line.Split('-');

graph.GetOrCreate(elements[0], () => new List<string>()).Add(elements[1]);
graph.GetOrCreate(elements[1], () => new List<string>()).Add(elements[0]);
}

List<string> biggest = [];
HashSet<string> visited = [];

foreach (string node in graph.Keys)
{
if (!visited.Add(node))
{
// already part of another clique
continue;
}

List<string> clique = [node];

foreach (string candidate in graph.Keys)
{
if (visited.Contains(candidate))
{
// already part of another clique
continue;
}

// if it's connected to every current member of the clique then it's allowed in
if (clique.All(member => graph[member].Contains(candidate)))
{
clique.Add(candidate);
visited.Add(candidate);
}
}

if (clique.Count > biggest.Count)
{
biggest = clique;
}
}

return string.Join(',', biggest.Order());
}

/// <summary>
/// Build the graph of every node to all the reachable nodes from that point
/// </summary>
/// <param name="input">Input edge descriptions</param>
/// <returns>Graph</returns>
private static SortedDictionary<string, ICollection<string>> BuildGraph(string[] input)
{
SortedDictionary<string, ICollection<string>> graph = new();

foreach (string line in input)
{
string[] elements = line.Split('-');

graph.GetOrCreate(elements[0], () => new List<string>()).Add(elements[1]);
graph.GetOrCreate(elements[1], () => new List<string>()).Add(elements[0]);
}

return graph;
}
}
}
Binary file added src/AdventOfCode/inputs/day23.txt
Binary file not shown.
105 changes: 105 additions & 0 deletions tests/AdventOfCode.Tests/Day23Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace AdventOfCode.Tests
{
public class Day23Tests
{
private readonly ITestOutputHelper output;
private readonly Day23 solver;

public Day23Tests(ITestOutputHelper output)
{
this.output = output;
this.solver = new Day23();
}

private static string[] GetRealInput()
{
string[] input = File.ReadAllLines("inputs/day23.txt");
return input;
}

private static string[] GetSampleInput()
{
return new string[]
{
"kh-tc",
"qp-kh",
"de-cg",
"ka-co",
"yn-aq",
"qp-ub",
"cg-tb",
"vc-aq",
"tb-ka",
"wh-tc",
"yn-cg",
"kh-ub",
"ta-co",
"de-co",
"tc-td",
"tb-wq",
"wh-td",
"ta-ka",
"td-qp",
"aq-cg",
"wq-ub",
"ub-vc",
"de-ta",
"wq-aq",
"wq-vc",
"wh-yn",
"ka-de",
"kh-ta",
"co-tc",
"wh-qp",
"tb-vc",
"td-yn",
};
}

[Fact]
public void Part1_SampleInput_ProducesCorrectResponse()
{
var expected = 7;

var result = solver.Part1(GetSampleInput());

Assert.Equal(expected, result);
}

[Fact]
public void Part1_RealInput_ProducesCorrectResponse()
{
var expected = 1599;

var result = solver.Part1(GetRealInput());
output.WriteLine($"Day 23 - Part 1 - {result}");

Assert.Equal(expected, result);
}

[Fact]
public void Part2_SampleInput_ProducesCorrectResponse()
{
var expected = "co,de,ka,ta";

var result = solver.Part2(GetSampleInput());

Assert.Equal(expected, result);
}

[Fact]
public void Part2_RealInput_ProducesCorrectResponse()
{
var expected = "av,ax,dg,di,dw,fa,ge,kh,ki,ot,qw,vz,yw";

var result = solver.Part2(GetRealInput());
output.WriteLine($"Day 23 - Part 2 - {result}");

Assert.Equal(expected, result);
}
}
}

0 comments on commit 3040c4e

Please sign in to comment.