private static int GetThrustSignal(params int[] phaseSettingInputs)
        {
            var amplifiers = Enumerable.Range(1, 5)
                             .Select(x => Intcode.Intcode.LoadFromFile("Day07/AmplifierControllerSoftware.txt"))
                             .ToArray();

            var phaseSettings = new PreparedInput(phaseSettingInputs);

            IInput input = new PreparedInput(0);
            IOPipe pipe  = null;

            foreach (var amplifier in amplifiers)
            {
                var amplifierInputs = new CombinedInput(phaseSettings, input);

                amplifier.SetInput(amplifierInputs);

                pipe = new IOPipe();
                amplifier.SetOutput(pipe);

                input = pipe;
            }

            foreach (var amplifier in amplifiers)
            {
                amplifier.Execute().Wait();
            }

            var result = pipe.ReadInput().Result;

            return(result);
        }
        private static IOPipe InitialisedPipe(int initialisedValue)
        {
            var pipe = new IOPipe();

            pipe.Output(initialisedValue);
            return(pipe);
        }
 private void InitialiseDrone()
 {
     _droneController = Intcode.LoadFromFile("Day19/DroneController.txt");
     _droneInput      = new IOPipe();
     _droneOutput     = new IOPipe();
     _droneController.SetInput(_droneInput);
     _droneController.SetOutput(_droneOutput);
 }
Example #4
0
 private void Initialise()
 {
     _ascii       = Intcode.LoadFromFile("Day17/ASCII.txt");
     _asciiInput  = new IOPipe();
     _asciiOutput = new IOPipe();
     _ascii.SetOutput(_asciiOutput);
     _ascii.SetInput(_asciiInput);
 }
Example #5
0
        static void DebugFile(string file_name, VM vm, int port)
        {
            //Console.WriteLine(Environment.CurrentDirectory);
            Console.WriteLine($"Simple Script {VM.Version}");
            try
            {
                var func = vm.Parse(File.ReadAllText(file_name), file_name);

                if (port > 0)
                {
                    TcpListener serverSocket = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
                    serverSocket.Start();
                    Console.WriteLine("DebugMode: Listen At {0}", port);

                    var socket = serverSocket.AcceptSocket();
                    if (socket != null)
                    {
                        Console.WriteLine("DebugMode: Build Connect With {0}", socket.RemoteEndPoint);
                        using (var stream = new NetworkStream(socket))
                        {
                            var pipe = new SimpleScript.DebugProtocol.NetServerPipe(stream);
                            vm.m_hooker.SetPipeServer(pipe);
                            vm.m_hooker.SetBreakMode(SimpleScript.DebugProtocol.BreakMode.StopForOnce);
                            vm.CallFunction(func);
                            // 测试协程,当成是事件循环也行
                            Console.WriteLine("Loop update for coroutine, You can close program to Exit!!");
                            while (true)
                            {
                                CoroutineMgr.Update();
                                System.Threading.Thread.Sleep(10);
                            }
                        }
                    }
                }
                else
                {
                    var pipe = new IOPipe();
                    vm.m_hooker.SetPipeServer(pipe);
                    vm.m_hooker.SetBreakMode(SimpleScript.DebugProtocol.BreakMode.StopForOnce);
                    vm.CallFunction(func);

                    // 测试协程,当成是事件循环也行
                    Console.WriteLine("Loop update for coroutine, You can close program to Exit!!");
                    while (true)
                    {
                        CoroutineMgr.Update();
                        System.Threading.Thread.Sleep(10);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Press Any Key To Exit");
            Console.ReadKey(true);
        }
Example #6
0
 public OxygenSystemLocater()
 {
     _repairDroid       = Intcode.LoadFromFile("Day15/RepairDroid.txt");
     _repairDroidInput  = new IOPipe();
     _repairDroidOutput = new IOPipe();
     _repairDroid.SetInput(_repairDroidInput);
     _repairDroid.SetOutput(_repairDroidOutput);
     _map = new Map();
 }
Example #7
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this)
            {
                PreferredBackBufferWidth  = 1344,
                PreferredBackBufferHeight = 608,

                SynchronizeWithVerticalRetrace = true
            };

            Content.RootDirectory = "Content";

            _io                   = new IOPipe();
            _computer             = new IntcodeComputer("Arcade", "input", _io);
            _io.FireEveryNbOutput = 3;

            _io.IntOuputted += OnComputerOutput;

            _io.ReadingInt += OnComputerRequestingInput;

            _computer.MemoryWriteAt(0, 2);
        }
Example #8
0
        static void Main(string[] args)
        {
            {
                //Type[] types = new Type[] {
                //    typeof(bool),
                //    typeof(char),
                //    typeof(byte), typeof(sbyte),
                //    typeof(ushort), typeof(short),
                //    typeof(uint), typeof(int),
                //    typeof(ulong), typeof(long),
                //    typeof(float), typeof(double),
                //};

                //foreach(var t in types)
                //{
                //    Console.WriteLine("{0}, {1}", t.IsPrimitive, t.IsValueType);
                //    var obj = Activator.CreateInstance(t, true);
                //    Console.WriteLine("{0}", obj);
                //}

                //return;
            }

            {
                SimpleScript.Test.TestManager.RunTest();
                //return;
            }

            {
                //ImportCodeGenerate.GenDelegateFactorySource("generate/DelegateFactory.cs", new Type[]{
                //    typeof(Func<int,int>),
                //    typeof(Action),
                //});

                //Console.WriteLine(typeof(Int64).IsPrimitive);
                VM vm = new VM();
                LibBase.Register(vm);
                LibCoroutine.Register(vm);

                vm.ComileFile("test.oms", "test.omsc");

                var pipe = new IOPipe();
                vm.m_hooker.SetPipeServer(pipe);
                vm.m_hooker.SetBreakMode(SimpleScript.DebugProtocol.BreakMode.StopForOnce);

                vm.DoFile("test.omsc");

                // 测试协程,当成是事件循环也行
                Console.Write("Start update coroutine!");
                if (Console.IsInputRedirected == false)
                {
                    Console.WriteLine(" Press Esc to end.");
                }
                else
                {
                    Console.WriteLine();
                }

                while (true)
                {
                    if (Console.IsInputRedirected == false && Console.KeyAvailable)
                    {
                        var key = Console.ReadKey(true);
                        if (key.Key == ConsoleKey.Escape)
                        {
                            break;
                        }
                    }
                    CoroutineMgr.Update();
                    System.Threading.Thread.Sleep(10);
                }

                return;
            }
        }
Example #9
0
        private static void Main(string[] args)
        {
            const int computerCount = 50;

            _buffers = new Dictionary <long, Queue <long[]> >();
            var computers = new IntcodeComputer[computerCount];
            var ios       = new IOPipe[computerCount];

            bool firstSolution = false;

            long lastNatX = -1, lastNatY = -1;
            long natX = 0, natY = 0;

            long      ticks          = 0;
            long      lastPacketTick = 0;
            const int idleThreshold  = 1250; // a little bit high just to be sure

            for (int i = 0; i < computerCount; i++)
            {
                var io = new IOPipe();
                int cp = i;
                ios[i] = io;
                io.InputInt(i);
                var buffer = new Queue <long[]>();

                _buffers.Add(i, buffer);

                computers[i] = new IntcodeComputer($"Computer {i}", "input", io);

                io.FireEveryNbOutput = 3;

                io.ReadingInt += (s, e) =>
                {
                    if (buffer.Count == 0)
                    {
                        io.InputInt(-1);
                    }
                    else
                    {
                        var packet = buffer.Dequeue();

                        io.InputInt(packet[0]);
                        io.InputInt(packet[1]);
                    }
                };

                io.IntOuputted += (s, e) =>
                {
                    var address = io.ReadOutputInt();
                    var X       = io.ReadOutputInt();
                    var Y       = io.ReadOutputInt();
                    lastPacketTick = ticks;

                    if (address == 255)
                    {
                        natX = X;
                        natY = Y;

                        if (!firstSolution)
                        {
                            Console.WriteLine(natY);
                            firstSolution = true;
                        }
                    }
                    else
                    {
                        _buffers[address].Enqueue(new long[] { X, Y });
                    }
                };
            }
            bool keepGoing = true;

            while (keepGoing)
            {
                foreach (var computer in computers)
                {
                    computer.Step();
                }

                if (_buffers.All(x => x.Value.Count == 0) && ticks - lastPacketTick > idleThreshold)
                {
                    if (lastNatY == natY)
                    {
                        Console.WriteLine(natY);
                        keepGoing = false;
                    }

                    _buffers[0].Enqueue(new[] { natX, natY });
                    lastPacketTick = ticks;

                    lastNatX = natX;
                    lastNatY = natY;
                }

                ticks++;
            }
        }