Skip to content

Commit

Permalink
Day 23 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaKateryna committed Dec 23, 2024
1 parent da57d33 commit 89a19ff
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions AdventOfCode/Day23.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace AdventOfCode;

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

HashSet<(string, string, string)> groups = new HashSet<(string, string, 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]));
}
}
}
}

int res = groups.Count(g => g.Item1.StartsWith("t") || g.Item2.StartsWith("t") || g.Item3.StartsWith("t"));

return new (res.ToString());
}

public override ValueTask<string> Solve_2()
{
return new ("");
}

private Dictionary<string, List<string>> ToDictionary(string[] lines)
{
Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();

foreach (string line in lines)
{
string[] computers = line.Split("-", StringSplitOptions.RemoveEmptyEntries);

if (!result.TryAdd(computers[0], new List<string>() { computers[1] }))
{
result[computers[0]].Add(computers[1]);
}

if (!result.TryAdd(computers[1], new List<string>() { computers[0] }))
{
result[computers[1]].Add(computers[0]);
}
}

return result;
}
}

0 comments on commit 89a19ff

Please sign in to comment.