Exemple #1
0
        private static (CharMap, int) BuildMapAndFindOxygen(string intcode)
        {
            var map = new CharMap();

            var movements = new MoveGenerator();

            map[movements.Current.Position] = MapSpace;

            var stepsToOxygen = 0;
            var debug         = false;

            var engine2 = new Engine()
                          .WithMemory(intcode)
                          .OnInput(engine =>
            {
                var movement = movements.NextProposal(map);
                if (movement == MoveNone)
                {
                    engine.Halt = true;
                }
                if (debug)
                {
                    Console.Clear();
                    Console.WriteLine($"Moves: {movements}");
                    foreach (var line in map.Render(MapOverlay))
                    {
                        Console.WriteLine(line);
                    }
                    Console.ReadKey();
                }
                engine.Input.Add(movement);
            })
                          .OnOutput(engine =>
            {
                var status = engine.Output.Take();
                switch (status)
                {
                case StatusHitTheWall:
                    map[movements.ProposedPosition] = MapWall;
                    break;

                case StatusMoved:
                    movements.ApproveMove();
                    map[movements.Current.Position] = MapSpace;
                    break;

                case StatusFoundOxygen:
                    movements.ApproveMove();
                    map[movements.Current.Position] = MapOxygen;
                    stepsToOxygen = movements.Moves;
                    break;
                }
            })
                          .Execute();

            return(map, stepsToOxygen);

            // Draw Droid on top of map
            char MapOverlay(Point p, char val)
            {
                var droid = movements.Current?.Position;

                return(p == droid ? MapDroid : val);
            }
        }