diff --git a/AdventOfCode/Problems/AOC2025/Day8/Playground.cs b/AdventOfCode/Problems/AOC2025/Day8/Playground.cs index 2536c55..c5a7cd1 100644 --- a/AdventOfCode/Problems/AOC2025/Day8/Playground.cs +++ b/AdventOfCode/Problems/AOC2025/Day8/Playground.cs @@ -9,49 +9,42 @@ namespace AdventOfCode.Problems.AOC2025.Day8; [ProblemInfo(2025, 8, "Playground")] internal class Playground : Problem { - private Vec3[] _boxPositions= []; + private Vec3i[] _boxPositions= []; public override void CalculatePart1() { - var boxes = _boxPositions.Select((p, i) => new JunctionBox(i, p, [])).ToArray(); + var networks = new List(); var closest = GetClosestPairs(_boxPositions, 1000); foreach (var (a, b)in closest) { - var boxA = boxes.First(box => box.Pos == a); - var boxB = boxes.First(box => box.Pos == b); - boxA.Connections.Add(boxB.Id); - boxB.Connections.Add(boxA.Id); + var existingNetworkA = networks.FirstOrDefault(n => n.Members.Contains(a)); + var existingNetworkB = networks.FirstOrDefault(n => n.Members.Contains(b)); + if ((existingNetworkA != null && existingNetworkB == null)) + existingNetworkA.AddConnection(a, b); + else if (existingNetworkB != null && existingNetworkA == null) + existingNetworkB.AddConnection(a, b); + else if (existingNetworkA != null && existingNetworkB != null && existingNetworkA != existingNetworkB) + { + existingNetworkA.AddConnection(a, b); + existingNetworkA.MergeWith(existingNetworkB); + networks.Remove(existingNetworkB); + } + else if(existingNetworkA == null && existingNetworkB == null) + { + var newNetwork = new Network().AddConnection(a, b); + networks.Add(newNetwork); + } } - var networks = TraceNetworks(boxes); - Part1 = networks.Select(n => (long)n.Count).OrderDescending().Take(3).Aggregate((a, b) => a * b); + Console.WriteLine($"Networks: {networks.Count}"); + Part1 = networks.Select(n => n.Members.Count) + .OrderDescending() + .Take(3) + .Aggregate((a, b) => a * b); } - private static List> TraceNetworks(JunctionBox[] junctionBoxes) + private static List<(Vec3i a , Vec3i b)> GetClosestPairs(Vec3i[] boxes, int count = 10) { - var networks = new List>(); - foreach (var box in junctionBoxes) - { - if (networks.Any(n => n.Contains(box.Id))) - continue; - var net = new HashSet() { 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 result) - { - foreach (var connection in box.Connections) - { - if (result.Add(connection)) - TraceJunction(boxes[connection], boxes, result); - } - } - - private static List<(Vec3 a , Vec3 b)> GetClosestPairs(Vec3[] boxes, int count = 10) - { - var distances = new Dictionary<(Vec3 a, Vec3 b), double>(); + var distances = new Dictionary<(Vec3i a, Vec3i b), double>(); for (int i = 0; i < boxes.Length; i++) { @@ -74,11 +67,63 @@ internal class Playground : Problem public override void LoadInput() { - _boxPositions = ReadInputLines("input.txt") + _boxPositions = ReadInputLines("sample.txt") .Select(l => l.Split(',').Select(int.Parse)) - .Select(c => new Vec3(c.First(), c.Skip(1).First(), c.Last())) + .Select(c => new Vec3i(c.First(), c.Skip(1).First(), c.Last())) .ToArray(); } - private record class JunctionBox(int Id, Vec3 Pos, HashSet Connections); + private record class JunctionBox(Vec3i Pos, HashSet Connections); + + private class Network + { + public HashSet Members { get; private set; } = []; + public List Boxes { get; private set; } = []; + + public bool IsConnectedTo(Vec3i pos) + { + return Members.Contains(pos); + } + + public bool IntersectsWith(Network other) + { + return Members.Intersect(other.Members).Any(); + } + + public Network AddConnection(Vec3i a, Vec3i b) + { + if (Members.Contains(a) && Members.Contains(b)) + return this; + Members.Add(a); + Members.Add(b); + var boxA = GetOrAddBox(a); + var boxB = GetOrAddBox(b); + boxA.Connections.Add(b); + boxB.Connections.Add(a); + return this; + } + + private JunctionBox GetOrAddBox(Vec3i pos) + { + var box = Boxes.FirstOrDefault(box => box.Pos == pos); + if(box == null) + { + box = new JunctionBox(pos, []); + Boxes.Add(box); + } + return box; + } + + public Network MergeWith(Network other) + { + foreach (var box in other.Boxes) + { + foreach (var connection in box.Connections) + { + AddConnection(box.Pos, connection); + } + } + return this; + } + } } diff --git a/AdventOfCode/Utils/Models/Common.cs b/AdventOfCode/Utils/Models/Common.cs index 712dd42..4c2250a 100644 --- a/AdventOfCode/Utils/Models/Common.cs +++ b/AdventOfCode/Utils/Models/Common.cs @@ -1,4 +1,10 @@ -using System.Numerics; +global using Vec3i = AdventOfCode.Utils.Models.Vec3; +global using Vec2i = AdventOfCode.Utils.Models.Vec2; +global using Vec3l = AdventOfCode.Utils.Models.Vec3; +global using Vec2l = AdventOfCode.Utils.Models.Vec2; +global using Vec3f = AdventOfCode.Utils.Models.Vec3; +global using Vec2f = AdventOfCode.Utils.Models.Vec2; +using System.Numerics; namespace AdventOfCode.Utils.Models; @@ -65,7 +71,7 @@ public record struct Vec3(T X, T Y, T Z) where T : INumber var a = other.X - this.X; var b = other.Y - this.Y; var c = other.Z - this.Z; - return (a * a) + (b * b) + (c * c); + return T.Abs((a * a) + (b * b) + (c * c)); } public readonly Vec3 Min(Vec3 other) => new(T.Min(X, other.X), T.Min(Y, other.Y), T.Min(Z, other.Z));