Exemplo n.º 1
0
        static async Task Main(string[] args)
        {
            var program = IntcodeMachine.ParseCode(File.ReadLines("input.txt").First());

            var mode = int.Parse(args.First());

            if (mode == 1)
            {
                var network = new SimpleNetworkSwitch();
                var tasks   = new List <Task>();
                for (var id = 0; id < 50; ++id)
                {
                    tasks.Add(StartProgram(program, id, network));
                }

                var result = await network.ResultTask;

                Console.WriteLine($"Part 1 Result = {result}");
            }
            else if (mode == 2)
            {
                var network = new NetworkSwitchMonitor(50);
                var tasks   = new List <Task>();
                for (var id = 0; id < 50; ++id)
                {
                    tasks.Add(StartProgram(program, id, network));
                }

                var result = await network.StartMonitor();

                Console.WriteLine($"Part 2 Result = {result}");
            }
        }
Exemplo n.º 2
0
        public static bool RunProgram(long[] program, Action <long> write, Func <long?> input = default)
        {
            var machine = new IntcodeMachine(program);

            while (true)
            {
                var result = machine.StepMany(input);
                switch (result.State)
                {
                case ExecuteState.HaveOutput:
                    write?.Invoke(result.Output.Value);
                    break;

                case ExecuteState.NeedInput:
                    return(false);

                case ExecuteState.Halted:
                    return(true);
                }
            }
        }