Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var input = await File.ReadAllLinesAsync("input.txt");
- var junctionBoxes = input
- .Select(line => line.Split(','))
- .Select(parts => new JunctionBox(long.Parse(parts[0]), long.Parse(parts[1]), long.Parse(parts[2])))
- .ToList();
- var distances = new List<(JunctionBox From, JunctionBox To, long Distance)>();
- for (var i = 0; i < junctionBoxes.Count - 1; i++)
- {
- var boxA = junctionBoxes[i];
- for (var j = i + 1; j < junctionBoxes.Count; j++)
- {
- var boxB = junctionBoxes[j];
- distances.Add((boxA, boxB, boxA.DistanceTo(boxB)));
- }
- }
- distances = [.. distances.OrderBy(d => d.Distance)];
- var circuits = new List<HashSet<JunctionBox>>();
- foreach (var box in junctionBoxes)
- {
- circuits.Add([box]);
- }
- var iteration = 0;
- while (true)
- {
- iteration++;
- var (from, to, _) = distances[iteration - 1];
- var circuitA = circuits.First(c => c.Contains(from));
- var circuitB = circuits.First(c => c.Contains(to));
- if (circuitA != circuitB)
- {
- circuitA.UnionWith(circuitB);
- circuits.Remove(circuitB);
- circuits = [.. circuits.OrderByDescending(c => c.Count)];
- }
- if (iteration == 1000)
- {
- Console.WriteLine($"Part 1: {circuits[0].Count * circuits[1].Count * circuits[2].Count}");
- }
- if (circuits.All(c => c.Count > 1))
- {
- Console.WriteLine($"Part 2: {from.X * to.X}");
- break;
- }
- }
- record struct JunctionBox(long X, long Y, long Z)
- {
- public readonly long DistanceTo(JunctionBox other) =>
- (long)Math.Sqrt(Math.Pow(X - other.X, 2) + Math.Pow(Y - other.Y, 2) + Math.Pow(Z - other.Z, 2));
- }
Advertisement
Add Comment
Please, Sign In to add comment