コード例 #1
0
        private async Task <int> Paint(SynchronousIntMachine intMachine, Dictionary <Point, int> canvas)
        {
            var paintedPositions = new HashSet <Point>();
            var directions       = new[] { new Point(-1, 0), new Point(0, 1), new Point(1, 0), new Point(0, -1) };
            var direction        = 0;
            var pos = new Point(0, 0);

            while (intMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed)
            {
                if (IsUpdateProgressNeeded())
                {
                    await UpdateProgressAsync();
                }

                var color = canvas.GetOrAdd(pos, _ => 0);
                intMachine.InputQueue.Enqueue(color);

                intMachine.RunUntilBlockOrComplete();
                canvas[pos] = (int)intMachine.OutputQueue.Dequeue();
                paintedPositions.Add(pos);

                intMachine.RunUntilBlockOrComplete();
                var directionDelta = (int)intMachine.OutputQueue.Dequeue() == 0 ? -1 : 1;
                direction = (direction + directionDelta + 4) % 4;
                pos      += directions[direction];
            }

            return(paintedPositions.Count);
        }
コード例 #2
0
ファイル: Day17.cs プロジェクト: sanraith/aoc2019
        public override async Task <string> Part2Async(string input)
        {
            var memory = IntMachineBase.ParseProgram(input);

            memory[0] = 2; // Wake up
            var intMachine = new SynchronousIntMachine(memory);
            var map        = await GetMap(intMachine);

            await UpdateProgressAsync(.5, 1);

            var(robotPos, robotChar) = map.First(x => RobotDirections.Contains(x.Value));
            var robotDirection = Directions[Array.IndexOf(RobotDirections, robotChar)];

            map[robotPos] = Scaffolding;

            myGarbageCounter = char.MaxValue;
            var path = await FollowPath(map, robotPos, robotDirection);

            var commandData = await FindSlicing(path.ToArray());

            var commands        = commandData.Select(x => ConvertCommand(x.Command)).ToArray();
            var movementRoutine = CreateMovementRoutine(commandData);

            movementRoutine.ForEach(x => intMachine.InputQueue.Enqueue(x));
            commands.SelectMany(x => x).ForEach(x => intMachine.InputQueue.Enqueue(x));
            "n\n".ForEach(x => intMachine.InputQueue.Enqueue(x));

            while (intMachine.RunUntilBlockOrComplete() != ReturnCode.Completed)
            {
            }

            return(intMachine.OutputQueue.Last().ToString());
        }
コード例 #3
0
        private static bool IsPulling(long [] memory, int x, int y)
        {
            var intMachine = new SynchronousIntMachine(memory);

            intMachine.InputQueue.Enqueue(x);
            intMachine.InputQueue.Enqueue(y);
            intMachine.RunUntilBlockOrComplete();
            return(intMachine.OutputQueue.Dequeue() == 1);
        }
コード例 #4
0
ファイル: 23.cs プロジェクト: kbmacneal/adv_of_code_2019
        private static (int Address, long X, long Y) ReadPacket(SynchronousIntMachine machine)
        {
            var o = machine.OutputQueue;

            while (o.Count < 3)
            {
                machine.RunUntilBlockOrComplete();
            }

            return((int)o.Dequeue(), o.Dequeue(), o.Dequeue());
        }
コード例 #5
0
ファイル: 23.cs プロジェクト: kbmacneal/adv_of_code_2019
 private static IEnumerable <(int Address, long X, long Y)> HandleOutgoingPackets(SynchronousIntMachine machine, Queue <long> [] queues, int MachineCount)
 {
     while (machine.RunUntilBlockOrComplete() == ReturnCode.WrittenOutput)
     {
         var packet = ReadPacket(machine);
         var(address, x, y) = packet;
         if (address < MachineCount)
         {
             queues [address].Enqueue(x);
             queues [address].Enqueue(y);
         }
         yield return(packet);
     }
 }
コード例 #6
0
        public static async Task Run()
        {
            var input = (await File.ReadAllTextAsync("inputs\\21.txt"));

            var myIntMachine = new SynchronousIntMachine(input);

            myIntMachine.addASCIILine("OR A J\n");
            myIntMachine.addASCIILine("AND B J\n");
            myIntMachine.addASCIILine("AND C J\n");
            myIntMachine.addASCIILine("NOT J J\n");
            myIntMachine.addASCIILine("AND D J\n");
            myIntMachine.addASCIILine("WALK\n");

            var c = 0;

            while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed)
            {
                c++;
            }

            Console.WriteLine("Part 1: " + myIntMachine.OutputQueue.Last());

            myIntMachine = new SynchronousIntMachine(input);

            myIntMachine.addASCIILine("OR A J\n");
            myIntMachine.addASCIILine("AND B J\n");
            myIntMachine.addASCIILine("AND C J\n");
            myIntMachine.addASCIILine("NOT J J\n");
            myIntMachine.addASCIILine("AND D J\n");
            myIntMachine.addASCIILine("OR I T\n");
            myIntMachine.addASCIILine("OR F T\n");
            myIntMachine.addASCIILine("AND E T\n");
            myIntMachine.addASCIILine("OR H T\n");
            myIntMachine.addASCIILine("AND T J\n");
            myIntMachine.addASCIILine("RUN\n");

            c = 0;

            while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed)
            {
                c++;
            }

            Console.WriteLine("Part 2: " + myIntMachine.OutputQueue.Last());
        }
コード例 #7
0
        private async Task <long> RunAsciiMachine(SynchronousIntMachine intMachine, string[] inputLines)
        {
            if (!IsInteractiveInput)
            {
                inputLines.Select(l => l + '\n').SelectMany(l => l).ForEach(c => intMachine.InputQueue.Enqueue(c));
            }

            ReturnCode returnCode;

            while ((returnCode = intMachine.RunUntilBlockOrComplete()) != ReturnCode.Completed)
            {
                if (IsUpdateProgressNeeded())
                {
                    await UpdateProgressAsync();
                }
                switch (returnCode)
                {
                case ReturnCode.WaitingForInput:
                    var userInput = Console.ReadLine() + '\n';
                    userInput.ToList().ForEach(x => intMachine.InputQueue.Enqueue(x));
                    break;

                case ReturnCode.WrittenOutput:
                    while (intMachine.OutputQueue.Count > 0)
                    {
                        var value = intMachine.OutputQueue.Dequeue();
                        if (value < 256)
                        {
                            if (IsInteractiveOutput)
                            {
                                Console.Write((char)value);
                            }
                        }
                        else
                        {
                            return(value);
                        }
                    }
                    break;
                }
            }
            return(-1);
        }
コード例 #8
0
ファイル: 17.cs プロジェクト: kbmacneal/adv_of_code_2019
        public static async Task Run()
        {
            var input = (await File.ReadAllTextAsync("inputs\\17.txt"));

            var myIntMachine = new SynchronousIntMachine(input);

            List <char> lines = new List <char> ();

            if (!File.Exists("outputs\\17.txt"))
            {
                while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed)
                {
                    var o = myIntMachine.OutputQueue.Dequeue();

                    lines.Add((char)o);
                }

                var sb = new StringBuilder();

                lines.ForEach(e => sb.Append(e));

                if (!Directory.Exists("outputs"))
                {
                    Directory.CreateDirectory("outputs");
                }

                await File.WriteAllTextAsync("outputs\\17.txt", sb.ToString(), Encoding.ASCII);
            }

            var scaffold = (await File.ReadAllLinesAsync("outputs\\17.txt", Encoding.ASCII)).Where(e => e != "").ToArray();

            var aparm = 0;

            for (int y = 0; y < scaffold.Length; y++)
            {
                for (int x = 0; x < scaffold [y].Length; x++)
                {
                    aparm += GetIntersect(x, y, scaffold);
                }
            }

            Console.WriteLine("Part 1: " + aparm.ToString());

            myIntMachine = new SynchronousIntMachine(input);

            myIntMachine.SetMemoryRegister(0, 2);

            var A = "L,12,L,8,R,10,R,10\n";
            var B = "L,6,L,4,L,12\n";
            var C = "R,10,L,8,L,4,R,10\n";

            var seq = "A,B,A,B,C,B,A,C,B,C\n";

            //use these to debug your input lengths
            //if you are stuck, try outputting the outputqueue after the vm is done running as ascii text
            // var la = A.ToCharArray ().Length;
            // var lb = B.ToCharArray ().Length;
            // var lc = C.ToCharArray ().Length;
            // var ls = seq.ToCharArray ().Length;

            myIntMachine.addASCIILine(seq);
            myIntMachine.addASCIILine(A);
            myIntMachine.addASCIILine(B);
            myIntMachine.addASCIILine(C);
            myIntMachine.addASCIILine("n\n");

            var c = 0;

            while (myIntMachine.RunUntilBlockOrComplete() != SynchronousIntMachine.ReturnCode.Completed)
            {
                c++;
            }

            var part2 = myIntMachine.OutputQueue.Last();

            Console.WriteLine("Part 2: " + part2.ToString());
        }
コード例 #9
0
ファイル: 13.cs プロジェクト: kbmacneal/adv_of_code_2019
        public static async Task Run()
        {
            var input = await File.ReadAllTextAsync("inputs\\13.txt");

            Dictionary <PointInt, tile_type> tiles = new Dictionary <PointInt, tile_type> ();

            var intMachine = new SynchronousIntMachine(input);

            while (intMachine.RunUntilBlockOrComplete() == ReturnCode.WrittenOutput)
            {
                while (intMachine.OutputQueue.Count < 3)
                {
                    intMachine.RunUntilBlockOrComplete();
                }
                var x    = (int)intMachine.OutputQueue.Dequeue();
                var y    = (int)intMachine.OutputQueue.Dequeue();
                var tile = intMachine.OutputQueue.Dequeue();

                tiles [new PointInt(x, y)] = (tile_type)tile;
            }

            var blocks = tiles.Where(e => e.Value == tile_type.Block).Count();

            Console.WriteLine("Part 1: " + blocks.ToString());

            intMachine = new SynchronousIntMachine(input);

            intMachine.SetMemoryRegister(0, 2);

            long       score      = 0;
            var        blockCount = blocks;
            var        ball       = Classes.Point.Empty;
            var        paddle     = Classes.Point.Empty;
            ReturnCode returnCode;

            while (blockCount > -1 && (returnCode = intMachine.RunUntilBlockOrComplete()) != ReturnCode.Completed)
            {
                switch (returnCode)
                {
                case ReturnCode.WaitingForInput:
                    if (blockCount == 0)
                    {
                        blockCount = -1; break;
                    }
                    var joystickInput = ball.X.CompareTo(paddle.X);
                    intMachine.InputQueue.Enqueue(joystickInput);
                    break;

                case ReturnCode.WrittenOutput:
                    while (intMachine.OutputQueue.Count < 3)
                    {
                        intMachine.RunUntilBlockOrComplete();
                    }
                    var x = (int)intMachine.OutputQueue.Dequeue();
                    var y = (int)intMachine.OutputQueue.Dequeue();
                    var t = intMachine.OutputQueue.Dequeue();

                    if (x == -1)
                    {
                        score = t;
                    }
                    else
                    {
                        var tile = (tile_type)t;
                        if (tile != tile_type.Block && tiles [new PointInt(x, y)] == tile_type.Block)
                        {
                            blockCount--;
                        }
                        tiles [new PointInt(x, y)] = tile;

                        if (tile == tile_type.Ball)
                        {
                            ball = new Classes.Point(x, y);
                        }
                        else if (tile == tile_type.HorizPaddle)
                        {
                            paddle = new Classes.Point(x, y);
                        }
                    }
                    break;
                }
            }

            Console.WriteLine("Part 2: " + score);
        }