Advertisement
Guest User

Star Clusters

a guest
Nov 25th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. namespace RegexEx
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public abstract class Point
  8.     {
  9.         private double x;
  10.         private double y;
  11.  
  12.         public Point(double x, double y)
  13.         {
  14.             this.X = x;
  15.             this.Y = y;
  16.         }
  17.  
  18.         public double X
  19.         {
  20.             get { return this.x; }
  21.             protected set { this.x = value; }
  22.         }
  23.  
  24.         public double Y
  25.         {
  26.             get { return this.y; }
  27.             protected set { this.y = value; }
  28.         }
  29.     }
  30.  
  31.     public class Star : Point
  32.     {
  33.         public Star(double x, double y) : base(x, y)
  34.         {
  35.         }
  36.     }
  37.  
  38.     public class Cluster : Point
  39.     {
  40.         private string name;
  41.         private List<Star> stars;
  42.  
  43.         public Cluster(string name, double x, double y) : base(x, y)
  44.         {
  45.             this.Name = name;
  46.             this.stars = new List<Star>();
  47.         }
  48.  
  49.         public string Name
  50.         {
  51.             get { return this.name; }
  52.             private set { this.name = value; }
  53.         }
  54.  
  55.         public void AddStar(Star star)
  56.         {
  57.             this.stars.Add(star);
  58.  
  59.             this.X = this.stars.Average(s => s.X);
  60.             this.Y = this.stars.Average(s => s.Y);
  61.         }
  62.  
  63.         public override string ToString()
  64.         {
  65.             return $"{this.Name} ({Math.Round(this.X)}, {Math.Round(this.Y)}) -> {this.stars.Count} stars";
  66.         }
  67.     }
  68.  
  69.     public class StarDistance
  70.     {
  71.         public StarDistance(string name, double distance)
  72.         {
  73.             this.StarName = name;
  74.             this.Distance = distance;
  75.         }
  76.  
  77.         public string StarName { get; }    
  78.  
  79.         public double Distance { get; }
  80.     }
  81.  
  82.     public class RegexEx
  83.     {
  84.         public static void Main(string[] args)
  85.         {
  86.             int numberOfClusters = int.Parse(Console.ReadLine());
  87.             List<Cluster> clusters = new List<Cluster>();
  88.             List<Star> stars = new List<Star>();
  89.  
  90.             for (int i = 0; i < numberOfClusters; i++)
  91.             {
  92.                 string[] input = Console.ReadLine().Split(new[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries);
  93.                 double x = double.Parse(input[1]);
  94.                 double y = double.Parse(input[2]);
  95.  
  96.                 Star star = new Star(x, y);
  97.                 Cluster cluster = new Cluster(input[0], x, y);
  98.  
  99.                 stars.Add(star);
  100.                 clusters.Add(cluster);
  101.             }
  102.  
  103.             List<StarDistance> distances = new List<StarDistance>();
  104.  
  105.             string command = Console.ReadLine();
  106.             while (command != "end")
  107.             {
  108.                 int[] tokens = command.Split(new[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries)
  109.                     .Select(int.Parse)
  110.                     .ToArray();
  111.  
  112.                 for (int i = 0; i < tokens.Length; i += 2)
  113.                 {
  114.                     int currentX = tokens[i];
  115.                     int currentY = tokens[i + 1];
  116.                     Star currentStar = new Star(currentX, currentY);
  117.  
  118.                     stars.Add(currentStar);
  119.                 }
  120.  
  121.                 command = Console.ReadLine();
  122.             }
  123.  
  124.             SplitStarsInClusters(clusters, stars, distances);
  125.  
  126.             foreach (var item in clusters.OrderBy(c => c.Name))
  127.             {
  128.                 Console.WriteLine(item);
  129.             }
  130.         }
  131.  
  132.         private static void SplitStarsInClusters(List<Cluster> clusters, List<Star> stars, List<StarDistance> distances)
  133.         {
  134.             for (int i = 0; i < stars.Count; i++)
  135.             {
  136.                 Star currentStar = stars[i];
  137.  
  138.                 foreach (var cluster in clusters)
  139.                 {
  140.                     //via Pythagorean theorem
  141.                     double sideA = currentStar.X - cluster.X;
  142.                     double sideB = currentStar.Y - cluster.Y;
  143.                     double sideC = Math.Sqrt(sideA * sideA + sideB * sideB);
  144.  
  145.                     StarDistance currentDistance = new StarDistance(cluster.Name, sideC);
  146.                     distances.Add(currentDistance);
  147.                 }
  148.  
  149.                 StarDistance closestDistance = distances.OrderBy(d => d.Distance).FirstOrDefault();
  150.                 clusters.Find(c => c.Name == closestDistance.StarName).AddStar(currentStar);
  151.                 distances.Clear();
  152.             }
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement