コード例 #1
0
        public int WallFollower(AsciiComputer cpu)
        {
            //cpu.EchoInput = true;
            //cpu.EchoOutput = true;
            var gameOutput = cpu.Run();

            var  facing          = Directions.north;
            bool securityVisited = false;

            while (true)
            {
                TakeAllItems(cpu, gameOutput);
                if (gameOutput.Contains("Security Checkpoint"))
                {
                    if (!securityVisited)
                    {
                        // first time here, turn around to get all items
                        facing          = (Directions)(((int)facing + 2) % 4);
                        securityVisited = true;
                    }
                    else
                    {
                        return(Force(cpu));
                    }
                }
                else
                {
                    var doors = GetDoors(gameOutput);
                    facing = TakeLeft(facing, doors);
                }

                gameOutput = cpu.Run(facing.ToString());
                //Console.ReadKey(true);
            }
        }
コード例 #2
0
        public int GetAnswer1(long[] program)
        {
            var cpu           = new IntCodeComputer(program);
            var asciiComputer = new AsciiComputer(cpu);

            return(WallFollower(asciiComputer));
        }
コード例 #3
0
        private bool ForceEntry(AsciiComputer cpu, ImmutableArray <string> inventory, ref int code)
        {
            // simple brute force try all combinations
            foreach (var item in inventory)
            {
                cpu.Run("drop " + item);
                var res = TryGetCode(cpu, ref code);
                if (res == 0)
                {
                    return(true);
                }
                if (res > 0)
                {
                    if (ForceEntry(cpu, inventory.Remove(item), ref code))
                    {
                        return(true);
                    }
                }

                cpu.Run("take " + item);
            }

            code = 0;
            return(false);
        }
コード例 #4
0
        private int Force(AsciiComputer cpu)
        {
            var all  = GetInventory(cpu);
            int code = 0;

            ForceEntry(cpu, all.ToImmutableArray(), ref code);
            return(code);
        }
コード例 #5
0
        public void TakeAllItems(AsciiComputer cpu, string gameOutput)
        {
            var itemsHere = GetList(gameOutput, "Items here:");

            foreach (var item in itemsHere.Except(UnsafeItems))
            {
                cpu.Run("take " + item);
            }
        }
コード例 #6
0
        private static long GetHullDamage(int[] input, string[] script)
        {
            var cpu = new IntCodeComputer(input);
            var asc = new AsciiComputer(cpu);

            foreach (var line in script)
            {
                var r = asc.Run(line.Trim());
                //Console.Write(r);
            }

            //Console.WriteLine(cpu.ToString());
            return(asc.ResultCode);
        }
コード例 #7
0
        public void Repl(long[] program)
        {
            while (true)
            {
                Console.WriteLine("****** Start New Game! ******");
                var cpu           = new IntCodeComputer(program);
                var asciiComputer = new AsciiComputer(cpu);

                Console.Write(asciiComputer.Run());
                while (!cpu.Finished)
                {
                    var input = Console.ReadLine();
                    if (input == "exit")
                    {
                        return;
                    }
                    Console.Write(asciiComputer.Run(input));
                }
            }
        }
コード例 #8
0
        private int TryGetCode(AsciiComputer cpu, ref int code)
        {
            var result = cpu.Run("south");

            if (!result.Contains("Alert!"))
            {
                code = int.Parse(Regex.Match(result, "\\d+").Value);
                return(0);
            }

            if (result.Contains("lighter"))
            {
                return(1);
            }
            if (result.Contains("heavier"))
            {
                return(-1);
            }

            throw new InvalidOperationException("unexpected result");
        }
コード例 #9
0
        private string[] GetInventory(AsciiComputer cpu)
        {
            var result = cpu.Run("inv");

            return(GetList(result, "Items in your inventory:"));
        }