Ejemplo n.º 1
0
        public Day20Room GetRoom(char direction)
        {
            switch (direction)
            {
            case 'W':
                return(west ?? (west = new Day20Room {
                    east = this
                }));

            case 'E':
                return(east ?? (east = new Day20Room {
                    west = this
                }));

            case 'N':
                return(north ?? (north = new Day20Room {
                    south = this
                }));

            case 'S':
                return(south ?? (south = new Day20Room {
                    north = this
                }));
            }

            throw new Exception("Unkonwn direction!");
        }
Ejemplo n.º 2
0
        public static Day20Room CreateMap(string input)
        {
            input = new string(input.SkipWhile(x => x.Equals('^')).TakeWhile(x => x != '$').ToArray());
            var startRoom = new Day20Room();

            Process(input, new List <Day20Room> {
                startRoom
            });
            startRoom.CalculateDistances();
            return(startRoom);

            List <Day20Room> Process(string a, List <Day20Room> inputRooms)
            {
                List <Day20Room> resultingRooms = new List <Day20Room>();
                List <Day20Room> currentRooms   = inputRooms.ToList();

                for (int i = 0; i < a.Length; i++)
                {
                    var character = a[i];
                    switch (character)
                    {
                    case '(':
                        int count = 1;
                        var sb    = new StringBuilder();
                        while (count != 0)
                        {
                            i++;
                            if (a[i] == '(')
                            {
                                count++;
                            }
                            else if (a[i] == ')')
                            {
                                count--;
                            }
                            sb.Append(a[i]);
                        }

                        resultingRooms.Clear();
                        var next = sb.ToString();
                        currentRooms = Process(next.Substring(0, next.Length - 1), currentRooms);
                        break;

                    case '|':
                        resultingRooms.AddRange(currentRooms);
                        currentRooms = inputRooms.ToList();
                        break;

                    case ')':
                        throw new Exception("Unexcepted input. Maybe mismatched parentheses?");

                    default:
                        currentRooms = currentRooms.Select(x => x.GetRoom(character)).ToList();
                        break;
                    }
                }
                return(resultingRooms.Distinct().ToList());
            }
        }