示例#1
0
        public string Solve(string data)
        {
            long[] memory = AsyncIntCodeVm.CreateMemoryFromProgramInput(data);

            var results = new List <(int A, int B, int C, int D, int E, long Output)>(100000);

            for (int phaseA = 5; phaseA < 10; phaseA++)
            {
                for (int phaseB = 5; phaseB < 10; phaseB++)
                {
                    for (int phaseC = 5; phaseC < 10; phaseC++)
                    {
                        for (int phaseD = 5; phaseD < 10; phaseD++)
                        {
                            for (int phaseE = 5; phaseE < 10; phaseE++)
                            {
                                if (phaseA == phaseB || phaseA == phaseC || phaseA == phaseD || phaseA == phaseE ||
                                    phaseB == phaseC || phaseB == phaseD || phaseB == phaseE ||
                                    phaseC == phaseD || phaseC == phaseE ||
                                    phaseD == phaseE)
                                {
                                    continue;
                                }

                                long result = this.GetOutput(memory, phaseA, phaseB, phaseC, phaseD, phaseE);
                                results.Add((phaseA, phaseB, phaseC, phaseD, phaseE, result));
                            }
                        }
                    }
                }
            }

            return(results.Max(x => x.Output).ToString());
        }
示例#2
0
        public void BasicTests(long[] memory, long input, int expectedDiagnosticCode)
        {
            var vm = new AsyncIntCodeVm(memory);

            long[] output = vm.Execute(input);
            Assert.That(output.Last(), Is.EqualTo(expectedDiagnosticCode));
        }
示例#3
0
        public string Solve(string data)
        {
            var vm = new AsyncIntCodeVm(data);

            vm.InputBuffer.Post(1);

            int direction        = 0;                     // Facing up
            var location         = new Point(0, 0);
            var paintedLocations = new HashSet <Point>(); // So we won't get dupes

            // Initialise with current location as white
            var hullLocationColours = new Dictionary <Point, long>()
            {
                { location, 1 },
            };

            Task vmTask = vm.ExecuteAsync();

            while (!vmTask.IsCompleted)
            {
                // Get the colour
                long colour;

                try
                {
                    colour = vm.OutputBuffer.Receive(TimeSpan.FromMilliseconds(10));
                }
                catch (Exception)
                {
                    if (vmTask.IsCompleted)
                    {
                        // Edge case. We managed to loop between reading the last value and before the program properly ended.
                        // This is OK, and we just break out of the loop and write the answer.
                        break;
                    }

                    throw;
                }

                // Paint
                hullLocationColours[location] = colour;
                paintedLocations.Add(location);

                // Get the location change
                long turn = vm.OutputBuffer.Receive(TimeSpan.FromMilliseconds(10));

                // Turn and move
                direction = (direction + (turn == 0 ? -1 : 1) + 4) % 4;
                location  = new Point(location.X + Directions[direction].X, location.Y + Directions[direction].Y);

                hullLocationColours.TryGetValue(location, out colour); // If there is no value, colour gets set to default(long), i.e. 0
                vm.InputBuffer.Post(colour);
            }

            // Build the visualisation; remove black locations from the list to ensure we only visualise what's needed.
            StringBuilder display = this.BuildVisualisation(hullLocationColours);

            return(display.ToString());
        }
示例#4
0
        public string Solve(string data)
        {
            var vm = new AsyncIntCodeVm(data);

            long[] outputs = vm.Execute(1);

            return(outputs.LastOrDefault().ToString());
        }
示例#5
0
        public string Solve(string data)
        {
            var vm = new AsyncIntCodeVm(data);

            long[] output = vm.Execute(2);

            return(output.Last().ToString());
        }
示例#6
0
        public string Solve(string data)
        {
            var vm = new AsyncIntCodeVm(data);

            vm.InputBuffer.Post(0);

            int direction           = 0;                     // Facing up
            var location            = new Point(0, 0);
            var paintedLocations    = new HashSet <Point>(); // So we won't get dupes
            var hullLocationColours = new Dictionary <Point, long>();

            Task vmTask = vm.ExecuteAsync();

            // Assuming we'll always get outputs in pairs. If not, we'd need to check between the two Receive calls.
            while (!vmTask.IsCompleted)
            {
                // Get the colour
                long colour = vm.OutputBuffer.Receive(TimeSpan.FromSeconds(1));

                // Paint
                hullLocationColours[location] = colour;
                paintedLocations.Add(location);

                // Get the location change
                long turn = vm.OutputBuffer.Receive(TimeSpan.FromSeconds(1));

                // Turn and move
                direction = (direction + (turn == 0 ? -1 : 1) + 4) % 4;
                location  = new Point(location.X + Directions[direction].X, location.Y + Directions[direction].Y);

                // Post colour of current location to VM
                hullLocationColours.TryGetValue(location, out colour); // If there is no value, colour gets set to default(long), i.e. 0
                vm.InputBuffer.Post(colour);
            }

            return(paintedLocations.Count.ToString());
        }