Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace RegexEx
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public abstract class Point
- {
- private double x;
- private double y;
- public Point(double x, double y)
- {
- this.X = x;
- this.Y = y;
- }
- public double X
- {
- get { return this.x; }
- protected set { this.x = value; }
- }
- public double Y
- {
- get { return this.y; }
- protected set { this.y = value; }
- }
- }
- public class Star : Point
- {
- public Star(double x, double y) : base(x, y)
- {
- }
- }
- public class Cluster : Point
- {
- private string name;
- private List<Star> stars;
- public Cluster(string name, double x, double y) : base(x, y)
- {
- this.Name = name;
- this.stars = new List<Star>();
- }
- public string Name
- {
- get { return this.name; }
- private set { this.name = value; }
- }
- public void AddStar(Star star)
- {
- this.stars.Add(star);
- this.X = this.stars.Average(s => s.X);
- this.Y = this.stars.Average(s => s.Y);
- }
- public override string ToString()
- {
- return $"{this.Name} ({Math.Round(this.X)}, {Math.Round(this.Y)}) -> {this.stars.Count} stars";
- }
- }
- public class StarDistance
- {
- public StarDistance(string name, double distance)
- {
- this.StarName = name;
- this.Distance = distance;
- }
- public string StarName { get; }
- public double Distance { get; }
- }
- public class RegexEx
- {
- public static void Main(string[] args)
- {
- int numberOfClusters = int.Parse(Console.ReadLine());
- List<Cluster> clusters = new List<Cluster>();
- List<Star> stars = new List<Star>();
- for (int i = 0; i < numberOfClusters; i++)
- {
- string[] input = Console.ReadLine().Split(new[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries);
- double x = double.Parse(input[1]);
- double y = double.Parse(input[2]);
- Star star = new Star(x, y);
- Cluster cluster = new Cluster(input[0], x, y);
- stars.Add(star);
- clusters.Add(cluster);
- }
- List<StarDistance> distances = new List<StarDistance>();
- string command = Console.ReadLine();
- while (command != "end")
- {
- int[] tokens = command.Split(new[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- for (int i = 0; i < tokens.Length; i += 2)
- {
- int currentX = tokens[i];
- int currentY = tokens[i + 1];
- Star currentStar = new Star(currentX, currentY);
- stars.Add(currentStar);
- }
- command = Console.ReadLine();
- }
- SplitStarsInClusters(clusters, stars, distances);
- foreach (var item in clusters.OrderBy(c => c.Name))
- {
- Console.WriteLine(item);
- }
- }
- private static void SplitStarsInClusters(List<Cluster> clusters, List<Star> stars, List<StarDistance> distances)
- {
- for (int i = 0; i < stars.Count; i++)
- {
- Star currentStar = stars[i];
- foreach (var cluster in clusters)
- {
- //via Pythagorean theorem
- double sideA = currentStar.X - cluster.X;
- double sideB = currentStar.Y - cluster.Y;
- double sideC = Math.Sqrt(sideA * sideA + sideB * sideB);
- StarDistance currentDistance = new StarDistance(cluster.Name, sideC);
- distances.Add(currentDistance);
- }
- StarDistance closestDistance = distances.OrderBy(d => d.Distance).FirstOrDefault();
- clusters.Find(c => c.Name == closestDistance.StarName).AddStar(currentStar);
- distances.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement