|
| 1 | +namespace AdventOfCode.Puzzles._2025._08.Part1; |
| 2 | + |
| 3 | +public class AoC2025Day8Part1 : IPuzzleSolution |
| 4 | +{ |
| 5 | + public async Task<string> SolveAsync(StreamReader inputReader) |
| 6 | + { |
| 7 | + var lines = await inputReader.ReadAllLinesAsync(); |
| 8 | + int targetConnections = lines.Count < 50 ? 10 : 1000; |
| 9 | + |
| 10 | + var boxes = new List<Point3d>(); |
| 11 | + foreach (var line in lines) |
| 12 | + { |
| 13 | + var parts = line.Split(',', StringSplitOptions.TrimEntries); |
| 14 | + boxes.Add(new Point3d |
| 15 | + { |
| 16 | + X = int.Parse(parts[0]), |
| 17 | + Y = int.Parse(parts[1]), |
| 18 | + Z = int.Parse(parts[2]), |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + var distinctDistances = new List<(Point3d box1, Point3d box2, double distance)>(); |
| 23 | + |
| 24 | + var distances = new Dictionary<(Point3d, Point3d), double>(); |
| 25 | + for (int box1 = 0; box1 < boxes.Count; box1++) |
| 26 | + { |
| 27 | + for (int box2 = box1 + 1; box2 < boxes.Count; box2++) |
| 28 | + { |
| 29 | + var distance = boxes[box1].Distance(boxes[box2]); |
| 30 | + distances[(boxes[box1], boxes[box2])] = distance; |
| 31 | + distances[(boxes[box2], boxes[box1])] = distance; |
| 32 | + distinctDistances.Add((boxes[box1], boxes[box2], distance)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + distinctDistances = distinctDistances |
| 37 | + .OrderBy(t => t.distance) |
| 38 | + .ToList(); |
| 39 | + |
| 40 | + var circuits = new Dictionary<Point3d, List<Point3d>>(); |
| 41 | + foreach (var box in boxes) |
| 42 | + { |
| 43 | + var circuit = new List<Point3d> { box }; |
| 44 | + circuits.Add(box, circuit); |
| 45 | + } |
| 46 | + |
| 47 | + for (var distanceId = 0; distanceId < targetConnections; distanceId++) |
| 48 | + { |
| 49 | + var (box1, box2, distance) = distinctDistances[distanceId]; |
| 50 | + |
| 51 | + var circuit1 = circuits[box1]; |
| 52 | + var circuit2 = circuits[box2]; |
| 53 | + if (circuit1 != circuit2) |
| 54 | + { |
| 55 | + // Merge circuits |
| 56 | + circuit1.AddRange(circuit2); |
| 57 | + foreach (var box in circuit2) |
| 58 | + { |
| 59 | + circuits[box] = circuit1; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + var largestCircuits = circuits |
| 65 | + .Select(c => c.Value) |
| 66 | + .Distinct() |
| 67 | + .OrderByDescending(c => c.Count) |
| 68 | + .Select(c => c.Count) |
| 69 | + .Take(3) |
| 70 | + .ToArray(); |
| 71 | + return (largestCircuits[0] * largestCircuits[1] * largestCircuits[2]).ToString(); |
| 72 | + } |
| 73 | +} |
0 commit comments