コード例 #1
0
        public async Task <long> ExecuteAsync()
        {
            var amplifiers = new LinkedList <IntcodeComputer>(_phases.Select(phase =>
            {
                var intcodeComputer = new IntcodeComputer(_code);
                intcodeComputer.Input(phase);
                return(intcodeComputer);
            }));

            var currentOutput = 0L;

            foreach (var amplifier in amplifiers)
            {
                var current = amplifiers.Find(amplifier);
                var next    = current?.Next ?? amplifiers.First;
                if (current != null)
                {
                    current.Value.OnOutput += output =>
                    {
                        if (next.Value.Program.Memory != null)
                        {
                            next.Value.Input(output);
                        }

                        currentOutput = output;
                    };
                }
            }

            amplifiers.First.Value.Input(_input);
            await Task.WhenAll(amplifiers.Select(amplifier => amplifier.StartAsync()));

            return(currentOutput);
        }
コード例 #2
0
ファイル: Day2.cs プロジェクト: mabargiel/AdventOfCode
        public long Part1()
        {
            var intcodeComputer = new IntcodeComputer(_input);

            intcodeComputer.Input(0);
            intcodeComputer.StartAsync().Wait();

            return(intcodeComputer.Program.Memory[0]);
        }
コード例 #3
0
ファイル: Day11.cs プロジェクト: mabargiel/AdventOfCode
        private Dictionary <Point, bool> RunHullRobot(long[] code, long input)
        {
            var paintArea       = new Dictionary <Point, bool>();
            var intcodeComputer = new IntcodeComputer(_robotCode);

            intcodeComputer.Input(input);
            var robot = new HullPaintingRobot(intcodeComputer, paintArea);

            robot.RunAsync().Wait();

            return(paintArea);
        }
コード例 #4
0
ファイル: Day9.cs プロジェクト: mabargiel/AdventOfCode
        public long[] Part1()
        {
            var results  = new List <long>();
            var computer = new IntcodeComputer(_instructions);

            computer.OnOutput += l => results.Add(l);
            if (_input != null)
            {
                computer.Input(_input.Value);
            }

            computer.StartAsync().Wait();

            return(results.ToArray());
        }
コード例 #5
0
ファイル: Day2.cs プロジェクト: mabargiel/AdventOfCode
        public long Part2()
        {
            var combinations = new Combinations <int>(Enumerable.Range(0, _input.Count()).ToList(), 2, GenerateOption.WithRepetition);

            var input = _input.ToArray();

            foreach (var combination in combinations)
            {
                var code     = (long[])input.Clone();
                var computer = new IntcodeComputer(code);
                computer.Input(0);
                computer.StartAsync().Wait();

                if (computer.Program.Memory[0] == _part2Target)
                {
                    return(100 * input[1] + input[2]);
                }

                input[1] = combination[0];
                input[2] = combination[1];
            }

            throw new ArgumentException("Could not find a noun and a verb to achieve the expected result");
        }