|
|
@@ -0,0 +1,100 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+
|
|
|
+namespace D25._1
|
|
|
+{
|
|
|
+ public class Star
|
|
|
+ {
|
|
|
+ public List<Star> Connections = new List<Star>();
|
|
|
+
|
|
|
+ public int[] Coords;
|
|
|
+
|
|
|
+ public override bool Equals(object obj)
|
|
|
+ {
|
|
|
+ var star = obj as Star;
|
|
|
+ return star != null && EqualityComparer<int[]>.Default.Equals(Coords, star.Coords);
|
|
|
+ }
|
|
|
+
|
|
|
+ public override int GetHashCode()
|
|
|
+ {
|
|
|
+ return Coords.GetHashCode();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class Program
|
|
|
+ {
|
|
|
+ static public int Manhattan(int[] a, int[] b)
|
|
|
+ {
|
|
|
+ int man = 0;
|
|
|
+ for (var i = 0; i < a.Length; ++i)
|
|
|
+ man += Math.Abs(a[i] - b[i]);
|
|
|
+ return man;
|
|
|
+ }
|
|
|
+
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ if (args.Length < 1) return;
|
|
|
+ if (File.Exists(args[0]) == false) return;
|
|
|
+
|
|
|
+ List<Star> all = new List<Star>();
|
|
|
+
|
|
|
+ var file = File.OpenText(args[0]);
|
|
|
+
|
|
|
+ do
|
|
|
+ {
|
|
|
+ var line = file.ReadLine();
|
|
|
+ if (line == null) break;
|
|
|
+
|
|
|
+ var coord = line.Split(",").Select(v => int.Parse(v)).ToArray();
|
|
|
+
|
|
|
+ var star = new Star()
|
|
|
+ {
|
|
|
+ Coords = coord
|
|
|
+ };
|
|
|
+
|
|
|
+ foreach (var a in all)
|
|
|
+ {
|
|
|
+ if (Manhattan(a.Coords, star.Coords) > 3) continue;
|
|
|
+ a.Connections.Add(star);
|
|
|
+ star.Connections.Add(a);
|
|
|
+ }
|
|
|
+
|
|
|
+ all.Add(star);
|
|
|
+
|
|
|
+ } while (true);
|
|
|
+
|
|
|
+ file.Close();
|
|
|
+
|
|
|
+ HashSet<Star> visited = new HashSet<Star>();
|
|
|
+ Queue<Star> queue = new Queue<Star>();
|
|
|
+
|
|
|
+ int constellations = 0;
|
|
|
+
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ var first = all.Where(a => visited.Contains(a) == false).FirstOrDefault();
|
|
|
+ if (first == null) break;
|
|
|
+
|
|
|
+ constellations++;
|
|
|
+ queue.Enqueue(first);
|
|
|
+
|
|
|
+ while (queue.Count > 0)
|
|
|
+ {
|
|
|
+ var s = queue.Dequeue();
|
|
|
+
|
|
|
+ visited.Add(s);
|
|
|
+ foreach (var c in s.Connections)
|
|
|
+ {
|
|
|
+ if (visited.Contains(c)) continue;
|
|
|
+ queue.Enqueue(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine($"The answer is : {constellations}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|