Skip to content

Commit

Permalink
day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
mazharenko committed Dec 23, 2024
1 parent 30d27c3 commit 9b0ebec
Show file tree
Hide file tree
Showing 3 changed files with 3,480 additions and 15 deletions.
113 changes: 98 additions & 15 deletions src/aoc/Year2024/Day23.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,102 @@ namespace aoc.Year2024;

internal partial class Day23
{
internal partial class Part1
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}

internal partial class Part2
{
public string Solve(string input)
{
throw new NotImplementedException();
}
}
private IList<HashSet<string>> CollectGroups((string from, string to)[] input)
{
var d =
input.Concat(input.Select(x => (x.to, x.from)))
.GroupBy(x => x.Item1, x => x.Item2)
.ToDictionary(x => x.Key, x => x.ToHashSet());

var all = input.SelectMany(x => new[] { x.from, x.to })
.ToHashSet();

var groups = new List<HashSet<string>>();
foreach (var comp in all)
{
var matchingGroups = groups.Where(
group => group.IsSubsetOf(d[comp]))
.ToList();

foreach (var matchingGroup in matchingGroups)
groups.Add([..matchingGroup, comp]);

groups.Add([comp]);
}

return groups;
}

internal partial class Part1
{
public Part1()
{
Expect(example, 7);
}

public int Solve((string to, string from)[] input)
{
return CollectGroups(input)
.Count(group => group.Count == 3 && group.Any(g => g.StartsWith('t')));
}
}

internal partial class Part2
{
public Part2()
{
Expect(example, "co,de,ka,ta");
}

public string Solve((string from, string to)[] input)
{
var groups = CollectGroups(input);
return string.Join(",", groups.MaxBy(group => group.Count)!.Order());
}
}

public (string, string)[] Parse(string input)
{
return Character.Letter.Many()
.ThenIgnore(Span.EqualTo("-"))
.Then(Character.Letter.Many())
.Select(x => (new string(x.Item1), new string(x.Item2)))
.Lines().Parse(input);
}

private readonly Example example = new(
"""
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
""");
}
2 changes: 2 additions & 0 deletions tests/AoC.Tests/InputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ private static IEnumerable<PartInputCaseData> GetCases()
yield return new PartInputCaseData(21, 2, "210686850124870");
yield return new PartInputCaseData(22, 1, "20401393616");
yield return new PartInputCaseData(22, 2, "2272");
yield return new PartInputCaseData(23, 1, "1302");
yield return new PartInputCaseData(23, 2, "cb,df,fo,ho,kk,nw,ox,pq,rt,sf,tq,wi,xz");
}
}
Loading

0 comments on commit 9b0ebec

Please sign in to comment.