day 8 wip

This commit is contained in:
2025-12-26 19:17:41 -05:00
parent 4acdf43eb1
commit 7460c1b70d

View File

@@ -13,8 +13,8 @@ internal class Playground : Problem<long, long>
public override void CalculatePart1() public override void CalculatePart1()
{ {
var boxes = _boxPositions.Select((p, i) => new JunctionBox(i, p, [])); var boxes = _boxPositions.Select((p, i) => new JunctionBox(i, p, [])).ToArray();
var closest = GetClosestPairs(_boxPositions, 10); var closest = GetClosestPairs(_boxPositions, 1000);
foreach (var (a, b)in closest) foreach (var (a, b)in closest)
{ {
var boxA = boxes.First(box => box.Pos == a); var boxA = boxes.First(box => box.Pos == a);
@@ -22,12 +22,31 @@ internal class Playground : Problem<long, long>
boxA.Connections.Add(boxB.Id); boxA.Connections.Add(boxB.Id);
boxB.Connections.Add(boxA.Id); boxB.Connections.Add(boxA.Id);
} }
var networks = TraceNetworks(boxes);
Part1 = networks.Select(n => (long)n.Count).OrderDescending().Take(3).Aggregate((a, b) => a * b);
} }
private static long[] TraceNetworks(JunctionBox[] junctionBoxes) private static List<HashSet<int>> TraceNetworks(JunctionBox[] junctionBoxes)
{ {
return []; var networks = new List<HashSet<int>>();
foreach (var box in junctionBoxes)
{
if (networks.Any(n => n.Contains(box.Id)))
continue;
var net = new HashSet<int>() { box.Id };
TraceJunction(box, junctionBoxes, net);
networks.Add(net);
}
return networks;//.Where(n => n.Count > 0).ToList();
}
private static void TraceJunction(JunctionBox box, JunctionBox[] boxes, HashSet<int> result)
{
foreach (var connection in box.Connections)
{
if (result.Add(connection))
TraceJunction(boxes[connection], boxes, result);
}
} }
private static List<(Vec3<int> a , Vec3<int> b)> GetClosestPairs(Vec3<int>[] boxes, int count = 10) private static List<(Vec3<int> a , Vec3<int> b)> GetClosestPairs(Vec3<int>[] boxes, int count = 10)
@@ -55,11 +74,11 @@ internal class Playground : Problem<long, long>
public override void LoadInput() public override void LoadInput()
{ {
_boxPositions = ReadInputLines("sample.txt") _boxPositions = ReadInputLines("input.txt")
.Select(l => l.Split(',').Select(int.Parse)) .Select(l => l.Split(',').Select(int.Parse))
.Select(c => new Vec3<int>(c.First(), c.Skip(1).First(), c.Last())) .Select(c => new Vec3<int>(c.First(), c.Skip(1).First(), c.Last()))
.ToArray(); .ToArray();
} }
private record JunctionBox(int Id, Vec3<int> Pos, HashSet<int> Connections); private record class JunctionBox(int Id, Vec3<int> Pos, HashSet<int> Connections);
} }