Пример #1
0
        protected override string Solve2(IEnumerable <string> inputs)
        {
            var cpu     = ServiceProvider.GetRequiredService <IntCodeCpu>();
            var program = inputs.First().ToProgram();

            var minutes   = 0;
            var maze      = ReadFile().Select(x => new MazeMovementOption(x.Split(","))).ToList();
            var hasOxygen = maze.Where(x => x.TileType == MazeTileType.Goal).ToList();

            while (hasOxygen.Any(x => x.Resolved == 0))
            {
                foreach (var tile in hasOxygen.Where(x => x.Resolved == 0).ToList())
                {
                    foreach (var neighbor in MazeWalker.GetNeighbors(tile.Position, maze))
                    {
                        if (neighbor.TileType == MazeTileType.Empty)
                        {
                            tile.Resolved = minutes;
                            hasOxygen.Add(neighbor);
                        }
                    }
                }

                minutes++;
            }

            MazeWalker.Render(maze);

            var result = maze.Max(x => x.Resolved);

            return(result.ToString());
        }
Пример #2
0
        public void move_eastTest()
        {
            List <List <string> > maze = generatemaze();

            MazeWalker walker = new MazeWalker(1, 1, Direction.East);

            Assert.IsTrue(walker.move_east(maze));

            walker.X = 2;
            walker.Y = 1;
            Assert.IsFalse(walker.move_east(maze));
        }
Пример #3
0
        public void move_westTest()
        {
            List <List <string> > maze = generatemaze();

            MazeWalker walker = new MazeWalker(2, 2, Direction.West);

            Assert.IsTrue(walker.move_west(maze));

            walker.X = 1;
            walker.Y = 2;
            Assert.IsFalse(walker.move_west(maze));
        }
Пример #4
0
        public void move_southTest()
        {
            List <List <string> > maze = generatemaze();

            MazeWalker walker = new MazeWalker(1, 1, Direction.South);

            Assert.IsTrue(walker.move_south(maze));

            walker.X = 1;
            walker.Y = 2;
            Assert.IsFalse(walker.move_south(maze));
        }
Пример #5
0
        public void move_northTest()
        {
            List <List <string> > maze = generatemaze();

            MazeWalker walker = new MazeWalker(2, 2, Direction.North);

            Assert.IsTrue(walker.move_north(maze));

            walker.X = 2;
            walker.Y = 1;
            Assert.IsFalse(walker.move_north(maze));
        }
Пример #6
0
        protected override string Solve(IEnumerable <string> inputs)
        {
            var program = inputs.First().ToProgram();
            var cpu     = ServiceProvider.GetRequiredService <IntCodeCpu>();

            var maze = new MazeWalker(cpu, program);

            maze.Run().Render();

            var data = new StringBuilder();

            foreach (var tile in maze.Tiles.Values.OrderBy(x => x.Position.X).ThenBy(x => x.Position.Y))
            {
                data.AppendLine($"{tile.Position.X},{tile.Position.Y},{tile.TileType}");
            }

            SaveToFile(data.ToString());

            return("Maze Rendered");
        }
Пример #7
0
        public async Task Walk_AllPossibleDirections_SuccessfullyPosted()
        {
            Guid mazeId = Guid.NewGuid();
            Path path   = new Path
            {
                MazeId      = mazeId,
                Source      = new Point(3, 3),
                Destination = new Point(3, 4),
                Steps       = new List <Point>
                {
                    new Point(3, 4),
                    new Point(4, 4),
                    new Point(4, 3),
                    new Point(3, 3),
                    new Point(3, 3),
                }
            };

            Mock <IPonyAPIClient> mockPonyAPI     = new Mock <IPonyAPIClient>();
            Mock <IMazeFactory>   mockMazeFactory = new Mock <IMazeFactory>();

            var directions = new List <string>();

            mockPonyAPI.Setup(m => m.PostStep(mazeId, Capture.In(directions)));
            Maze maze = CreateMaze(width: 15,
                                   height: 15,
                                   difficulty: 10,
                                   ponyPosition: new Point(-1, -1),
                                   endPoint: path.Destination,
                                   monsterPosition: new Point(10, 10)
                                   );

            maze.MazeId = mazeId;
            mockMazeFactory.Setup(m => m.FromID(mazeId)).ReturnsAsync(maze);

            mazeWalker = new MazeWalker(mockPonyAPI.Object, mockMazeFactory.Object);

            await mazeWalker.Walk(path);

            Assert.AreEqual(new[] { "south", "east", "north", "west", "stay" }, directions);
        }
Пример #8
0
        public void Descend()
        {
            depth += 1;

            if (maze != null)
            {
                maze.OnChange -= new Maze.ChangeHandler(passalongHandler);
            }
            maze           = MazeFactory.MakeMaze(30, 30, depth);
            maze.OnChange += new Maze.ChangeHandler(passalongHandler);

            if (walker != null)
            {
                walker.OnChange -= new MazeWalker.ChangeHandler(passalongHandler);
            }
            walker           = new MazeWalker(maze[random.Next(maze.Width), random.Next(maze.Height)]);
            walker.OnChange += new MazeWalker.ChangeHandler(passalongHandler);

            if (this.OnChange != null)
            {
                this.OnChange(this);
            }
        }
Пример #9
0
        public async Task Walk_WithMonster_MonsterAvoided()
        {
            // Arrange

            /* Maze with Endpoint and Monster with this positions:
             * ----|D
             * P
             * ----
             *     |E
             *
             * For the test, the monster will only move when P is about to cross.
             */
            Maze maze = CreateMaze(width: 15,
                                   height: 15,
                                   difficulty: 10,
                                   ponyPosition: new Point(1, 2),
                                   endPoint: new Point(5, 3),
                                   monsterPosition: new Point(5, 1),
                                   walls: new List <Point>
            {
                new Point(2, 1),
                new Point(2, 3),
                new Point(3, 1),
                new Point(3, 3),
                new Point(4, 1),
                new Point(4, 3),
                new Point(5, 0),
                new Point(5, 4),
            }
                                   );
            Path path = new Path
            {
                MazeId      = maze.MazeId,
                Source      = maze.Pony.Position,
                Destination = maze.EndPoint,
                Steps       = new List <Point>
                {
                    new Point(2, 2),
                    new Point(3, 2),
                    new Point(4, 2),
                    new Point(5, 2),
                    new Point(5, 3),
                }
            };

            Mock <IPonyAPIClient> mockPonyAPI    = new Mock <IPonyAPIClient>();
            Point monsterRightOfPony             = new Point(maze.Domokun.Position.X, maze.Domokun.Position.Y + 1);
            Point monsterOnTopOfPony             = new Point(maze.Domokun.Position.X - 1, maze.Domokun.Position.Y + 1);
            Point monsterLeftOfPony              = new Point(maze.Domokun.Position.X - 2, maze.Domokun.Position.Y + 1);
            MovingMonsterMazeFactory mazeFactory = new MovingMonsterMazeFactory(maze, new List <Point>
            {
                maze.Domokun.Position,
                maze.Domokun.Position,
                maze.Domokun.Position,
                maze.Domokun.Position,
                // Domokun starts walking
                monsterRightOfPony,
                monsterOnTopOfPony,
                // Domokun goes back, pony shoud wait more
                monsterRightOfPony,
                monsterOnTopOfPony,
                // Domokun goes past the pony
                monsterLeftOfPony,
                monsterLeftOfPony,
                monsterLeftOfPony,
            });

            var directions = new List <string>();

            mockPonyAPI.Setup(m => m.PostStep(maze.MazeId, Capture.In(directions)));

            mazeWalker = new MazeWalker(mockPonyAPI.Object, mazeFactory);

            // Act
            await mazeWalker.Walk(path);

            // Assert
            Assert.AreEqual(new[] { "east", "east", "east", "stay", "stay", "stay", "stay", "stay", "east", "south" }, directions);
        }