예제 #1
0
        public static void Execute(string input)
        {
            long[] code = Array.ConvertAll(input.Split(','), long.Parse);
            _game = new IntcodeComputer(code, true).WriteMem(0, 2).Run();

            Console.Clear();

            ParseOutput();
            int blocks = 0;

            foreach (long tile in Screen)
            {
                if (tile == 2)
                {
                    blocks++;
                }
            }

            int frameCounter = 0;

            while (_game.GetState != IntcodeComputer.State.Halted)
            {
                _game.Run();
                ParseOutput();
                if (frameCounter % 2 == 0)
                {
                    DrawScreen();
                }
                Thread.Sleep(8);
                if (_paddle > _ball)
                {
                    _game.QueueInput(-1);
                }
                else if (_paddle < _ball)
                {
                    _game.QueueInput(1);
                }
                else
                {
                    _game.QueueInput(0);
                }
                frameCounter++;
            }

            Console.Clear();
            Console.WriteLine($"Part 1: {blocks}");
            Console.WriteLine($"Part 2: {_score}");
        }
예제 #2
0
        public static void Execute(string input)
        {
            long[] code = Array.ConvertAll(input.Split(','), long.Parse);

            IntcodeComputer cpu = new IntcodeComputer(code).QueueInput(1).Run();
            long            a   = 0;

            while (cpu.OutputQueue.Count > 0)
            {
                a = cpu.OutputQueue.Dequeue();
            }
            Console.WriteLine($"Part 1: {a}");

            long b = new IntcodeComputer(code).QueueInput(5).Run().OutputQueue.Dequeue();

            Console.WriteLine($"Part 2: {b}");
        }
예제 #3
0
        private static RunReturnValue RunComputer(long[] code, int defaultValue)
        {
            IntcodeComputer            robot = new IntcodeComputer(code, true).Run();
            Dictionary <IntPoint, int> map   = new Dictionary <IntPoint, int>();


            IntPoint  l   = new IntPoint(0, 0);
            IntPoint  min = new IntPoint(int.MaxValue, int.MaxValue);
            IntPoint  max = new IntPoint(int.MinValue, int.MinValue);
            Direction d   = Direction.Up;

            while (robot.GetState != IntcodeComputer.State.Halted)
            {
                int currentSquare = map.ContainsKey(l) ? map[l] : defaultValue;
                int newSquare     = (int)robot.QueueInput(currentSquare).Run().OutputQueue.Dequeue();
                int turn          = (int)robot.OutputQueue.Dequeue();

                map[l] = newSquare;
                d      = MakeTurn(d, turn);

                l += DirectionToVector(d);
                if (l.X > max.X || l.Y > max.Y)
                {
                    max = new IntPoint(Math.Max(l.X, max.X), Math.Max(l.Y, max.Y));
                }
                if (l.X < min.X || l.Y < max.Y)
                {
                    min = new IntPoint(Math.Min(l.X, min.X), Math.Min(l.Y, min.Y));
                }

                //DrawCenter(map, l, d, map.Count, $"After move");
                //Thread.Sleep(10);
            }

            RunReturnValue ret = new RunReturnValue();

            ret.Map     = map;
            ret.Min     = min;
            ret.Max     = max;
            ret.Cycles  = robot.Cycles;
            ret.Seconds = robot.ExecutionTime.TotalSeconds;
            return(ret);
        }
예제 #4
0
        public static void Execute(string input)
        {
            long[] code = Array.ConvertAll(input.Split(','), long.Parse);

            long highest = -1;

            foreach (var list in GetPermutations(new List <int> {
                0, 1, 2, 3, 4
            }, 5))
            {
                var  permutation = (List <int>)list;
                long a           = new IntcodeComputer(code).QueueInput(permutation[0]).QueueInput(0).Run().OutputQueue.Dequeue();
                long b           = new IntcodeComputer(code).QueueInput(permutation[1]).QueueInput(a).Run().OutputQueue.Dequeue();
                long c           = new IntcodeComputer(code).QueueInput(permutation[2]).QueueInput(b).Run().OutputQueue.Dequeue();
                long d           = new IntcodeComputer(code).QueueInput(permutation[3]).QueueInput(c).Run().OutputQueue.Dequeue();
                long e           = new IntcodeComputer(code).QueueInput(permutation[4]).QueueInput(d).Run().OutputQueue.Dequeue();
                if (e > highest)
                {
                    highest = e;
                }
            }

            Console.WriteLine($"Part 1: {highest}");

            highest = -1;
            foreach (var list in GetPermutations(new List <int> {
                5, 6, 7, 8, 9
            }, 5))
            {
                var  permutation            = (List <int>)list;
                long lastE                  = -1;
                long a                      = 0;
                IntcodeComputer[] computers =
                {
                    new IntcodeComputer(code, true).QueueInput(permutation[0]),
                    new IntcodeComputer(code, true).QueueInput(permutation[1]),
                    new IntcodeComputer(code, true).QueueInput(permutation[2]),
                    new IntcodeComputer(code, true).QueueInput(permutation[3]),
                    new IntcodeComputer(code, true).QueueInput(permutation[4]),
                };
                for (int i = 0;; i++)
                {
                    IntcodeComputer computer = computers[i % 5];
                    computer.QueueInput(a).Run();
                    a = computer.OutputQueue.Dequeue();
                    if (i % 5 == 4)
                    {
                        lastE = a;
                    }

                    if (computers.Count(cpu => cpu.GetState == IntcodeComputer.State.Halted) == 5)
                    {
                        break;
                    }
                }

                if (lastE > highest)
                {
                    highest = lastE;
                }
            }

            Console.WriteLine($"Part 2: {highest}");
        }