示例#1
0
        public void EnterInput(VacuumRobot vr, string inputs)
        {
            Console.WriteLine(inputs);
            List <long> ascii = inputs.Select(x => Convert.ToInt64(x)).ToList();

            foreach (long input in ascii)
            {
                vr.Input = input;
                vr.Run();
            }

            vr.Input = 10;
            vr.Run();
        }
示例#2
0
        public Dictionary <Point, long> GetCameraFeed(VacuumRobot vr)
        {
            int x = 0;
            int y = 0;

            Dictionary <Point, long> cameraFeed = new Dictionary <Point, long>();

            do
            {
                vr.Run();
                long output = vr.Output;

                cameraFeed.Add(new Point(x, y), vr.Output);

                if (vr.Output == 10)
                {
                    x = 0;
                    y++;
                }
                else
                {
                    x++;
                }
            } while (vr.CurrentState != IntcodeComputer.State.Halted);

            return(cameraFeed);
        }
示例#3
0
        public string SolvePartTwo(string[] input)
        {
            long[] cameraFeedProgram = string.Join(",", input)
                                       .Split(",").Select(x => Int64.Parse(x)).ToArray();
            long[] wakeupProgram = string.Join(",", input)
                                   .Split(",").Select(x => Int64.Parse(x)).ToArray();

            VacuumRobot vr = new VacuumRobot(cameraFeedProgram, false, true);
            Dictionary <Point, long> scaffolding = GetCameraFeed(vr)
                                                   .Where(x => x.Value != 46 && x.Value != 10)
                                                   .ToDictionary(x => x.Key, x => x.Value);

            ScaffoldPathBuilder spb = new ScaffoldPathBuilder();

            StringBuilder sb = new StringBuilder();

            spb.GetScaffoldInstructions(scaffolding)
            .Select(x => Convert.ToChar(x))
            .ToList().ForEach(c => sb.Append(c));

            string mainMovRoutine = sb.ToString();
            string movFunctionA   = ReduceInstructions(ref mainMovRoutine, "A", 18, "ABC");
            string movFunctionB   = ReduceInstructions(ref mainMovRoutine, "B", 20, "ABC");
            string movFunctionC   = ReduceInstructions(ref mainMovRoutine, "C", 6, "ABC");

            wakeupProgram[0] = 2;
            vr = new VacuumRobot(wakeupProgram, true, false);
            vr.Run();
            EnterInput(vr, mainMovRoutine);
            EnterInput(vr, movFunctionA);
            EnterInput(vr, movFunctionB);
            EnterInput(vr, movFunctionC);
            EnterInput(vr, "n");

            while (vr.CurrentState != IntcodeComputer.State.Halted)
            {
                vr.Run();
            }

            Console.WriteLine();

            return(vr.Output.ToString());
        }