Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using System.Net.Sockets;
  8.  
  9. namespace Robotos.cs
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. int port = 12005;
  16. TcpClient client;
  17. try
  18. {
  19. client = new TcpClient("ecovpn.dyndns.org", port);
  20. } catch (Exception e)
  21. {
  22. Console.Error.WriteLine(e.ToString());
  23. Console.ReadKey();
  24. return;
  25. }
  26.  
  27. List<string> instructions = new List<string>();
  28.  
  29. Point armPos = new Point(500, 500);
  30.  
  31. string boxData = "";
  32.  
  33. using (StreamReader data = new StreamReader("Robots.csv"))
  34. {
  35. data.ReadLine();
  36. boxData = data.ReadLine();
  37. }
  38.  
  39. instructions.Add("start;BVe1wrvIuzvW;" + boxData);
  40.  
  41. Point[] boxes = Point.StringToPoints(boxData);
  42.  
  43. Point WF = MathX.Weiszfeld(boxes).GetRounded();
  44.  
  45. List<Point> possibleCenter = new List<Point>();
  46.  
  47. if (WF.Distance(Point.zero) > 300)
  48. possibleCenter.Add(WF);
  49.  
  50. for (int i = 0; i < boxes.Length; i++)
  51. {
  52. for (int x = -(int)Math.Floor(boxes.Length / 2f); x < (int)Math.Ceiling(boxes.Length / 2f) - 1; x++)
  53. {
  54. Point cur = boxes[i] + Point.right * 100 * x;
  55.  
  56. Point checkA = cur + Point.right * 100 * boxes.Length, checkB = cur + Point.right * 100 * -boxes.Length;
  57. float distA = checkA.Distance(Point.zero), distB = checkB.Distance(Point.zero);
  58.  
  59. if (distA <= 1200 && distA > 300 && distB <= 1200 && distB > 300 && !possibleCenter.Contains(cur))
  60. possibleCenter.Add(cur);
  61. }
  62.  
  63. for (int y = -(int)Math.Floor(boxes.Length / 2f); y < (int)Math.Ceiling(boxes.Length / 2f) - 1; y++)
  64. {
  65. Point cur = boxes[i] + Point.up * 100 * y;
  66.  
  67. Point checkA = cur + Point.up * 100 * boxes.Length, checkB = cur + Point.up * 100 * -boxes.Length;
  68. float distA = checkA.Distance(Point.zero), distB = checkB.Distance(Point.zero);
  69.  
  70. if (distA <= 1200 && distA > 300 && distB <= 1200 && distB > 300 && !possibleCenter.Contains(cur))
  71. possibleCenter.Add(cur);
  72. }
  73. }
  74.  
  75. possibleCenter = possibleCenter.OrderBy(center => MathX.GetScore(boxes, center).a).ToList();
  76.  
  77. Point best = possibleCenter[0];
  78. Pair<float, string> line = MathX.GetScore(boxes, best);
  79.  
  80. List<Point> left;
  81. if (line.b == "X")
  82. left = boxes.OrderBy(box => Math.Abs(box.y)).ToList();
  83. else
  84. left = boxes.OrderBy(box => Math.Abs(box.x)).ToList();
  85. List<Point> posLeft = new List<Point>();
  86.  
  87. for (int i = -(int)Math.Floor(boxes.Length / 2f); i < (int)Math.Floor(boxes.Length / 2f) - (boxes.Length % 2 == 0 ? 1 : 0); i++)
  88. {
  89. Point possible;
  90.  
  91. if (line.b == "X")
  92. possible = best + Point.right * 50 * i;
  93. else
  94. possible = best + Point.right * 50 * i;
  95.  
  96. bool found = false;
  97.  
  98. for (int n = 0; n < boxes.Length; n++)
  99. {
  100. if (boxes[n] == possible)
  101. {
  102. found = true;
  103. left.Remove(boxes[n]);
  104. }
  105. }
  106.  
  107. if (found)
  108. break;
  109.  
  110. posLeft.Add(possible);
  111. }
  112.  
  113. for (int i = 0; i < posLeft.Count; i++)
  114. {
  115. left = left.OrderBy(box => MathX.LineIntersectCirlce(new Pair<Point, Point>(box, posLeft[i]), Point.zero, 325) ? 1 : 0).ThenBy(box => posLeft[i].Distance(box)).ToList();
  116.  
  117. Point cur = left[0];
  118.  
  119. if (MathX.LineIntersectCirlce(new Pair<Point, Point>(armPos, cur), Point.zero, 300))
  120. {
  121. armPos = MathX.FindTangentIntersection(cur, armPos, Point.zero, 300).GetRounded();
  122. instructions.Add("move;BVe1wrvIuzvW;" + armPos);
  123. }
  124. instructions.Add("move;BVe1wrvIuzvW;" + cur);
  125. instructions.Add("pickup;BVe1wrvIuzvW");
  126.  
  127. if (MathX.LineIntersectCirlce(new Pair<Point, Point>(posLeft[i], cur), Point.zero, 325)) {
  128. cur = MathX.FindTangentIntersection(cur, posLeft[i], Point.zero, 325).GetRounded();
  129. instructions.Add("move;BVe1wrvIuzvW;" + cur);
  130. }
  131.  
  132. bool col = false;
  133. for (int n = 0; n < boxes.Length; n++)
  134. {
  135. if (boxes[n] == left[0])
  136. continue;
  137.  
  138. if (MathX.LineIntersectsBox(new Pair<Point, Point>(posLeft[i], left[0]), boxes[n]))
  139. {
  140. col = true;
  141. break;
  142. }
  143. }
  144.  
  145. if (col)
  146. {
  147. if (line.b == "X")
  148. {
  149. instructions.Add("move;BVe1wrvIuzvW;" + (posLeft[i] + Point.right * (cur.x - line.a)));
  150. armPos = posLeft[i];
  151. } else
  152. {
  153. instructions.Add("move;BVe1wrvIuzvW;" + (posLeft[i] + Point.right * (cur.y - line.a)));
  154. }
  155. }
  156. instructions.Add("putdown;BVe1wrvIuzvW");
  157. instructions.Add("move;BVe1wrvIuzvW;" + (posLeft[i]));
  158. armPos = posLeft[i];
  159. left.RemoveAt(0);
  160. }
  161.  
  162. instructions.Add("end;BVe1wrvIuzvW");
  163.  
  164. NetworkStream stream = client.GetStream();
  165. foreach (string i in instructions)
  166. {
  167. Byte[] data = Encoding.ASCII.GetBytes(i);
  168. stream.Write(data, 0, data.Length);
  169.  
  170. Console.WriteLine("Sent: " + i);
  171.  
  172. data = new Byte[256];
  173. string responseData = String.Empty;
  174.  
  175. int bytes = stream.Read(data, 0, data.Length);
  176. responseData = Encoding.ASCII.GetString(data, 0, bytes);
  177. Console.WriteLine("Recieved: " + responseData + '\n');
  178. }
  179. stream.Close();
  180. client.Close();
  181.  
  182. Console.ReadKey();
  183. }
  184. }
  185.  
  186. class Point
  187. {
  188. public float x, y;
  189.  
  190. public float Length { get { return (float)Math.Sqrt(x * x + y * y); } }
  191.  
  192. public static Point zero = new Point(0, 0);
  193. public static Point one = new Point(1, 1);
  194. public static Point right = new Point(1, 0);
  195. public static Point up = new Point(0, 1);
  196.  
  197. public Point (float x, float y)
  198. {
  199. this.x = x;
  200. this.y = y;
  201. }
  202.  
  203. public Point (string data)
  204. {
  205. data = data.Substring(1, data.Length - 2);
  206. string[] xy = data.Split(',');
  207. x = int.Parse(xy[0]);
  208. y = int.Parse(xy[1]);
  209. }
  210.  
  211. public static Point[] StringToPoints (string data)
  212. {
  213. data = data.Substring(1, data.Length - 2);
  214. string[] points = data.Split(';');
  215. Point[] rtrn = new Point[points.Length];
  216.  
  217. for (int i = 0; i < points.Length; i++)
  218. rtrn[i] = new Point(points[i]);
  219.  
  220. return rtrn;
  221. }
  222.  
  223. public Point GetRounded ()
  224. {
  225. return new Point((float)Math.Round(x), (float)Math.Round(y));
  226. }
  227.  
  228. public float Distance (Point B)
  229. {
  230. return (this - B).Length;
  231. }
  232.  
  233. public float Cross(Point b)
  234. {
  235. return x * b.y - y * b.x;
  236. }
  237.  
  238. public static Point operator + (Point A, Point B)
  239. {
  240. return new Point(A.x + B.x, A.y + B.y);
  241. }
  242.  
  243. public static Point operator -(Point A, Point B)
  244. {
  245. return new Point(A.x - B.x, A.y - B.y);
  246. }
  247.  
  248. public static Point operator *(Point A, float b)
  249. {
  250. return new Point(A.x * b, A.y * b);
  251. }
  252.  
  253. public static Point operator /(Point A, float b)
  254. {
  255. return new Point(A.x / b, A.y / b);
  256. }
  257.  
  258. public override string ToString()
  259. {
  260. return "(" + x + "," + y + ")";
  261. }
  262.  
  263. public override bool Equals(object obj)
  264. {
  265. if (obj.GetType() != typeof(Point))
  266. return false;
  267.  
  268. Point other = (Point)obj;
  269.  
  270. return other.x == x && other.y == y;
  271. }
  272.  
  273. public override int GetHashCode()
  274. {
  275. return base.GetHashCode();
  276. }
  277. }
  278.  
  279. class MathX
  280. {
  281. static int WFAccuracy = 10;
  282.  
  283. public static Point Weiszfeld (Point[] boxes)
  284. {
  285. Point P = new Point(0, 0);
  286.  
  287. for (int n = 0; n < WFAccuracy; n++)
  288. {
  289. Point numerator = new Point(0, 0);
  290. float denominator = 0;
  291. for (int i = 0; i < boxes.Length; i++)
  292. {
  293. numerator += boxes[i] / (P - boxes[i]).Length;
  294. denominator += 1 / (P - boxes[i]).Length;
  295. }
  296.  
  297. P = numerator / denominator;
  298. }
  299.  
  300. return P;
  301. }
  302.  
  303. static float GetMovTime (Point start, Point end)
  304. {
  305. float s = start.Distance(end);
  306. float t = (float)Math.Sqrt(2400 * s)/(2 * s);
  307. if (t * 1200 <= 250)
  308. return t;
  309. else
  310. {
  311. s -= 625 / 24f;
  312. return 5 / 24f + s / 250;
  313. }
  314. }
  315.  
  316. public static Pair<float, string> GetScore (Point[] boxes, Point center)
  317. {
  318. List<Point> Xtemp = boxes.ToList();
  319. List<Point> Ytemp = new List<Point>(Xtemp);
  320.  
  321. Point curPos = new Point(500, 500);
  322.  
  323. Pair<float, string> min = new Pair<float, string>(float.PositiveInfinity, "");
  324.  
  325. float curTime = 0;
  326.  
  327. for (int x = -(int)Math.Floor(boxes.Length / 2f); x < (int)Math.Ceiling(boxes.Length / 2f) - 1; x++)
  328. {
  329. Point cur = center + Point.right * 100 * x;
  330.  
  331. float dist = cur.Distance(Point.zero);
  332.  
  333. if (dist <= 300 || dist > 1200)
  334. {
  335. curTime = float.PositiveInfinity;
  336. break;
  337. }
  338.  
  339. Xtemp = Xtemp.OrderBy(box => -box.Distance(cur)).ToList();
  340.  
  341. if (cur.Distance(Xtemp[0]) == 0)
  342. continue;
  343.  
  344. curTime += GetMovTime(curPos, Xtemp[0]) + GetMovTime(Xtemp[0], cur) + 4;
  345. curPos = cur;
  346. Xtemp.RemoveAt(0);
  347. }
  348.  
  349. if (curTime != float.PositiveInfinity)
  350. {
  351. min.a = curTime;
  352. min.b = "X";
  353. }
  354.  
  355. curTime = 0;
  356. curPos = Point.one * 500;
  357.  
  358. for (int y = -(int)Math.Floor(boxes.Length / 2f); y < (int)Math.Ceiling(boxes.Length / 2f) - 1; y++)
  359. {
  360. Point cur = center + Point.up * 100 * y;
  361.  
  362. float dist = cur.Distance(Point.zero);
  363.  
  364. if (dist <= 300 || dist > 1200)
  365. {
  366. curTime = float.PositiveInfinity;
  367. break;
  368. }
  369.  
  370. Ytemp = Ytemp.OrderBy(box => -box.Distance(cur)).ToList();
  371.  
  372. if (cur.Distance(Ytemp[0]) == 0)
  373. continue;
  374.  
  375. curTime += GetMovTime(curPos, Ytemp[0]) + GetMovTime(Ytemp[0], cur) + 4;
  376. curPos = cur;
  377. Ytemp.RemoveAt(0);
  378. }
  379.  
  380. if (curTime < min.a)
  381. {
  382. min.a = curTime;
  383. min.b = "Y";
  384. }
  385.  
  386. return min;
  387. }
  388.  
  389. public static bool LineIntersectCirlce (Pair<Point, Point> line, Point center, float r)
  390. {
  391. float x = line.a.Distance(line.b);
  392. return ((center.x - line.a.x) * (line.b.y - line.a.y) - (center.y - line.a.y) * (line.b.x - line.a.x)) / x <= r;
  393. }
  394.  
  395. public static bool LineIntersectLine (Pair<Point, Point> line, Pair<Point, Point> line2)
  396. {
  397. Point p = line.a;
  398. Point r = line.b - p;
  399. Point q = line2.a;
  400. Point s = line2.b - q;
  401.  
  402. float t = (q - p).Cross(s) / r.Cross(s);
  403. float u = (q - p).Cross(r) / (r.Cross(s));
  404.  
  405. if (r.Cross(s) == 0 && (q - p).Cross(r) == 0)
  406. return false;
  407. if (r.Cross(s) == 0 && (q - p).Cross(r) != 0)
  408. return false;
  409. if (r.Cross(s) != 0 && t >= 0 && t <= 1 && u >= 0 && u <= 1)
  410. return true;
  411. return false;
  412. }
  413.  
  414. public static bool LineIntersectsBox (Pair<Point, Point> line, Point box)
  415. {
  416. Point A, B, C, D;
  417. A = box += Point.one * 50;
  418. B = box += new Point(50, -50);
  419. C = box -= Point.one * 50;
  420. D = box -= new Point(50, -50);
  421.  
  422. Pair<Point, Point> lineA, lineB, lineC, lineD;
  423. lineA = new Pair<Point, Point>(A, B);
  424. lineB = new Pair<Point, Point>(B, C);
  425. lineC = new Pair<Point, Point>(C, D);
  426. lineD = new Pair<Point, Point>(D, A);
  427.  
  428. Func<Pair<Point, Point>, bool> quickTest = l => LineIntersectLine(line, l);
  429.  
  430. return quickTest(lineA) || quickTest(lineB) || quickTest(lineC) || quickTest(lineD);
  431. }
  432.  
  433. public static float Sec (float x)
  434. {
  435. return 1 / (float)Math.Sin(x);
  436. }
  437.  
  438. public static Point RayRayIntersect (Point A, Point B, float dirA, float dirB)
  439. {
  440. Point dirVB = new Point((float)Math.Cos(dirA), (float)Math.Sin(dirA));
  441. Point dirVA = new Point((float)Math.Cos(dirB), (float)Math.Sin(dirB));
  442.  
  443. float u = ((B.y - A.y) * dirVB.x - (B.x - A.x) * dirVB.y) / (dirVB.x * dirVA.y - dirVB.y * dirVA.x);
  444. float v = ((B.y - A.y) * dirVA.x - (B.x - A.x) * dirVA.y) / (dirVB.x * dirVA.y - dirVB.y * dirVA.x);
  445.  
  446. if ((A + dirVA * u).Distance(B + dirVB * v) > 0.01)
  447. return null;
  448. return A + dirVA * u;
  449. }
  450.  
  451. public static Point FindTangentIntersection (Point A, Point B, Point center, float r)
  452. {
  453. float A1 = (float)Math.Asin(r / A.Distance(center));
  454. float angAC = (float)Math.Atan((center - A).y / (center - A).x);
  455. float angA1 = angAC - A1;
  456. float angA2 = angAC + A1;
  457. float B1 = (float)Math.Asin(r / B.Distance(center));
  458. float angBC = (float)Math.Atan((center - B).y / (center - B).x);
  459. float angB1 = angBC - B1;
  460. float angB2 = angBC + B1;
  461.  
  462. Point T1 = RayRayIntersect(A, B, angA1, angB2);
  463. Point T2 = RayRayIntersect(A, B, angA2, angB1);
  464.  
  465. T1 = new Point(T1.y, T1.x);
  466. T2 = new Point(T2.y, T2.x);
  467.  
  468. Func<Point, float> quickCheck = p => { return A.Distance(p) + p.Distance(B); };
  469.  
  470. if (quickCheck(T1) < quickCheck(T2))
  471. return T1;
  472. return T2;
  473. }
  474. }
  475.  
  476. public class Pair <U, T>
  477. {
  478. public U a;
  479. public T b;
  480.  
  481. public Pair (U a, T b)
  482. {
  483. this.a = a;
  484. this.b = b;
  485. }
  486. }
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement