using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace D10._2 { class Map : List<(int x, int y, int vx, int vy)> { } class Program { static void Main(string[] args) { if (args.Length < 1) return; if (File.Exists(args[0]) == false) return; var file = File.OpenText(args[0]); var r = new Regex(@"position=<\s*(?-?\d+),\s*(?-?\d+)> velocity=<\s*(?-?\d+),\s*(?-?\d+)>"); var map = new Map(); do { var line = file.ReadLine(); if (line == null) break; var result = r.Match(line); map.Add(( int.Parse(result.Groups["posx"].Value), int.Parse(result.Groups["posy"].Value), int.Parse(result.Groups["vx"].Value), int.Parse(result.Groups["vy"].Value) )); } while (true); int tt = 0; do { for (var i = 0; i < map.Count; ++i) { var star = map[i]; star.x += star.vx; star.y += star.vy; map[i] = star; } tt++; } while (isOk(map) == false); Console.WriteLine($"Answer is : {tt}s"); } static bool isOk(Map map) { for (var i = 0; i < map.Count; ++i) if (isIsolated(map, i)) return false; return true; } static bool isIsolated(Map map, int current) { var currentStar = map[current]; foreach (var star in map) { if (star.x == currentStar.x && star.y == currentStar.y) continue; if (star.x == currentStar.x && (star.y == currentStar.y + 1 || star.y == currentStar.y - 1)) return false; if (star.y == currentStar.y && (star.x == currentStar.x + 1 || star.x == currentStar.x - 1)) return false; if ((star.x == currentStar.x + 1 || star.x == currentStar.x - 1) && (star.y == currentStar.y + 1 || star.y == currentStar.y - 1)) return false; if ((star.y == currentStar.y + 1 || star.y == currentStar.y - 1) && (star.x == currentStar.x + 1 || star.x == currentStar.x - 1)) return false; } return true; } } }