Exemplo n.º 1
0
        public void Step(Railway railway)
        {
            var prevPoint = new Point(X, Y);

            // Move forward
            ForwardVector.AddVectorToPoint(this);

            var track = railway.Tracks[X, Y];

            var neighbours = track.Neighbours();

            // Dont go backwards
            neighbours.Remove(prevPoint);

            // Pick direction
            if (neighbours.Count == 1)
            {
                ForwardVector = Vector.VectorBetweenPoints(this, neighbours[0]);
            }
            else if (neighbours.Count == 3)
            {
                switch (NextDirection)
                {
                case Direction.LEFT:
                    ForwardVector.RotateLeft();
                    NextDirection = Direction.UP;
                    break;

                case Direction.UP:
                    NextDirection = Direction.RIGHT;
                    break;

                case Direction.RIGHT:
                    ForwardVector.RotateRight();
                    NextDirection = Direction.LEFT;
                    break;

                default:
                    throw new Exception("Abnormal NextDirection");
                }
            }
            else
            {
                throw new Exception("Abnormal number of neighbours");
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            List <string> lines = new List <string>(File.ReadAllLines(@"input.txt"));

            // Pick part 1 or 2
            var part = 2;

            var width  = lines[0].Length;
            var height = lines.Count();

            var railway = new Railway(width, height);

            char[] horizontalChars = new char[] { '-', '>', '<', '+' };     // + could be ambiguous e.g. "+\+", but looks like the input avoids this
            char[] verticalChars   = new char[] { '|', 'v', '^', '+' };

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    switch (lines[y][x])
                    {
                    case '-':
                        railway.SetTrack(new Track(x, y, Direction.LEFT | Direction.RIGHT));
                        break;

                    case '|':
                        railway.SetTrack(new Track(x, y, Direction.UP | Direction.DOWN));
                        break;

                    case '\\':
                        if (x == width - 1 || y == 0 || (x > 0 && horizontalChars.Contains(lines[y][x - 1])) || (y < height - 1 && verticalChars.Contains(lines[y + 1][x])))
                        {
                            railway.SetTrack(new Track(x, y, Direction.LEFT | Direction.DOWN));
                        }
                        else if (x == 0 || y == height - 1 || (x < width - 1 && horizontalChars.Contains(lines[y][x + 1])) || (y > 0 && verticalChars.Contains(lines[y - 1][x])))
                        {
                            railway.SetTrack(new Track(x, y, Direction.RIGHT | Direction.UP));
                        }
                        else
                        {
                            throw new Exception("Ambiguous character");
                        }
                        break;

                    case '/':
                        if (x == width - 1 || y == height - 1 || (x > 0 && horizontalChars.Contains(lines[y][x - 1])) || (y > 0 && verticalChars.Contains(lines[y - 1][x])))
                        {
                            railway.SetTrack(new Track(x, y, Direction.LEFT | Direction.UP));
                        }
                        else if (x == 0 || y == 0 || (x < width - 1 && horizontalChars.Contains(lines[y][x + 1])) || (y < height - 1 && verticalChars.Contains(lines[y + 1][x])))
                        {
                            railway.SetTrack(new Track(x, y, Direction.RIGHT | Direction.DOWN));
                        }
                        else
                        {
                            throw new Exception("Ambiguous character");
                        }
                        break;

                    case '+':
                        railway.SetTrack(new Track(x, y, Direction.LEFT | Direction.UP | Direction.DOWN | Direction.RIGHT));
                        break;

                    case '^':
                        railway.SetTrack(new Track(x, y, Direction.UP | Direction.DOWN));
                        railway.Trains.Add(new Train(x, y, Direction.UP));
                        break;

                    case '<':
                        railway.SetTrack(new Track(x, y, Direction.LEFT | Direction.RIGHT));
                        railway.Trains.Add(new Train(x, y, Direction.LEFT));
                        break;

                    case 'v':
                        railway.SetTrack(new Track(x, y, Direction.UP | Direction.DOWN));
                        railway.Trains.Add(new Train(x, y, Direction.DOWN));
                        break;

                    case '>':
                        railway.SetTrack(new Track(x, y, Direction.LEFT | Direction.RIGHT));
                        railway.Trains.Add(new Train(x, y, Direction.RIGHT));
                        break;

                    default:
                        break;
                    }
                }
            }

            Train train = null;

            if (part == 1)
            {
                while (railway.CollisionTrains.Count() == 0)
                {
                    railway.Step();
                }

                train = railway.CollisionTrains.First();
            }
            else
            {
                while (railway.Trains.Count > 1)
                {
                    railway.Step();
                }

                train = railway.Trains.First();
            }
            Console.WriteLine("Result: {0},{1}", train.X, train.Y);
        }