Skip to content

Commit

Permalink
Day 23 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 23, 2024
1 parent a10c3a1 commit 461b201
Showing 1 changed file with 67 additions and 25 deletions.
92 changes: 67 additions & 25 deletions AdventOfCode/Day23.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
namespace AdventOfCode;
using System.Text.RegularExpressions;

namespace AdventOfCode;

public class Day23 : BaseDay
{
public override ValueTask<string> Solve_1()
{
string[] lines = File.ReadAllLines(InputFilePath);

HashSet<string> groups = new HashSet<string>();
Dictionary<string, List<string>> dictionary = ToDictionary(lines);

foreach (var kv in dictionary)
{
string computer1 = kv.Key;
if (kv.Value.Count < 2)
{
continue;
}
for (int i = 0; i < kv.Value.Count - 1; ++i)
{
for (int j = i + 1; j < kv.Value.Count; ++j)
{
if (dictionary[kv.Value[i]].Contains(kv.Value[j]))
{
var group = new List<string> { computer1, kv.Value[i], kv.Value[j] };
group = group.OrderBy(x => x).ToList();
groups.Add($"{group[0]},{group[1]},{group[2]}");
}
}
}
}
HashSet<string> groups = GetSetsOfThree(lines, dictionary);

int res = groups.Count(g => g.Split(",").Any(x => x.StartsWith("t")));

Expand All @@ -37,7 +17,17 @@ public override ValueTask<string> Solve_1()

public override ValueTask<string> Solve_2()
{
return new ("");
string[] lines = File.ReadAllLines(InputFilePath);
Dictionary<string, List<string>> dictionary = ToDictionary(lines);
HashSet<string> groups = GetSetsOfThree(lines, dictionary);

while (groups.Count != 1)
{
groups = GetGroupsPlusOne(groups, dictionary);
Console.WriteLine(groups.Count);
}

return new (groups.Single().ToString());
}

private Dictionary<string, List<string>> ToDictionary(string[] lines)
Expand All @@ -61,4 +51,56 @@ private Dictionary<string, List<string>> ToDictionary(string[] lines)

return result;
}

private HashSet<string> GetSetsOfThree(string[] lines, Dictionary<string, List<string>> dictionary)
{
HashSet<string> groups = new HashSet<string>();

foreach (var kv in dictionary)
{
string computer1 = kv.Key;
if (kv.Value.Count < 2)
{
continue;
}
for (int i = 0; i < kv.Value.Count - 1; ++i)
{
for (int j = i + 1; j < kv.Value.Count; ++j)
{
if (dictionary[kv.Value[i]].Contains(kv.Value[j]))
{
var group = new List<string> { computer1, kv.Value[i], kv.Value[j] };
group = group.OrderBy(x => x).ToList();
groups.Add($"{group[0]},{group[1]},{group[2]}");
}
}
}
}
return groups;
}

private HashSet<string> GetGroupsPlusOne(HashSet<string> groups, Dictionary<string, List<string>> dictionary)
{
HashSet<string> groupsPlusOne = new HashSet<string>();
foreach (var group in groups)
{
List<string> computers = group.Split(",").ToList();
foreach (var computer in dictionary.Keys)
{
if (computers.Contains(computer))
{
continue;
}
int intersect = dictionary[computer].Intersect(computers).Count();
if (intersect == computers.Count)
{
var newGroup = computers.Concat(new List<string> { computer });
newGroup = newGroup.OrderBy(x => x).ToList();
groupsPlusOne.Add(string.Join(",", newGroup));
}
}
}

return groupsPlusOne;
}
}

0 comments on commit 461b201

Please sign in to comment.