예제 #1
0
        public static Yard Parse(String input)
        {
            int width, height;


            string[] lines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            if (!lines[0].Contains("x"))
            {
                throw new FormatException("First line should contain the dimensions of the grid");
            }

            {
                string[] temp = lines[0].Split('x');
                width  = int.Parse(temp[0]);
                height = int.Parse(temp[1]);
            }

            Yard yard = new Yard(width, height);

            {
                Regex tracks     = new Regex(@"(?<name>T[\w]+?) (?<length>[0-9]+)m on \((?<x>[0-9]+),\s?(?<y>[0-9]+)\) w(?<size>[0-9]+)");
                var   collection = tracks.Matches(input);

                foreach (Match match in collection)
                {
                    String name   = match.Groups["name"].Value;
                    int    length = int.Parse(match.Groups["length"].Value);
                    int    x      = int.Parse(match.Groups["x"].Value);
                    int    y      = int.Parse(match.Groups["y"].Value);
                    int    w      = int.Parse(match.Groups["size"].Value);

                    yard.tracks[name] = new Track(name, length, new Point(x, y), w);
                }
            }

            {
                Regex switches   = new Regex(@"(?<name>W[\w]+?) on \((?<x>[0-9]+),\s?(?<y>[0-9]+)\)");
                var   collection = switches.Matches(input);

                foreach (Match match in collection)
                {
                    String name = match.Groups["name"].Value;
                    int    x    = int.Parse(match.Groups["x"].Value);
                    int    y    = int.Parse(match.Groups["y"].Value);

                    yard.switches[name] = new Switch(name, x, y);
                }
            }

            {
                Regex lrx = new Regex(@"l\((?<id>\w+?)\)");
                Regex rrx = new Regex(@"r\((?<id>\w+?)\)");
                Regex nrx = new Regex(@"(?<id>\w+)");
                Func <String, Point> extract = s =>
                {
                    var lm = lrx.Match(s);
                    if (lm.Success)
                    {
                        String id    = lm.Groups["id"].Value;
                        Track  track = yard.tracks[id];
                        return(new Point(track.left.X, track.left.Y));
                    }
                    var rm = rrx.Match(s);
                    if (rm.Success)
                    {
                        String id    = rm.Groups["id"].Value;
                        Track  track = yard.tracks[id];
                        return(new Point(track.right.X, track.right.Y));
                    }
                    var nm = nrx.Match(s);
                    if (nm.Success)
                    {
                        String id = nm.Groups["id"].Value;
                        Switch sw = yard.switches[id];
                        return(new Point(sw.X, sw.Y));
                    }

                    throw new FormatException($"{s} was invalid format");
                };

                foreach (var line in lines)
                {
                    if (!line.StartsWith("---"))
                    {
                        continue;
                    }

                    string[] entry = line.Replace("---", "").Split(new[] { " - " }, StringSplitOptions.None);

                    var one = extract(entry[0]);
                    var two = extract(entry[1]);

                    yard.connections.Add(new Connection(one, two));
                }
            }

            return(yard);
        }