Пример #1
0
        private int GetThrusterOutput(IEnumerable <int> phaseSettings, string program, bool feedback)
        {
            var A = new IntCodeVM(program);
            var B = new IntCodeVM(program);
            var C = new IntCodeVM(program);
            var D = new IntCodeVM(program);
            var E = new IntCodeVM(program);

            A.OutputVM = B;
            B.OutputVM = C;
            C.OutputVM = D;
            D.OutputVM = E;

            if (feedback)
            {
                E.OutputVM = A;
            }

            A.AddInput(phaseSettings.ElementAt(0));
            B.AddInput(phaseSettings.ElementAt(1));
            C.AddInput(phaseSettings.ElementAt(2));
            D.AddInput(phaseSettings.ElementAt(3));
            E.AddInput(phaseSettings.ElementAt(4));

            A.Run(0);

            return(E.Outputs.Last());
        }
Пример #2
0
        public override string PartOne(string input)
        {
            var vm     = new IntCodeVM(input);
            var output = vm.Run(1);

            return(output.Last().ToString());
        }
Пример #3
0
    public override string Part1()
    {
        var vms = Enumerable.Range(0, 50)
                  .Select((_, i) =>
        {
            var vm = new IntCodeVM(Input.First());
            vm.Run(i);
            return(vm);
        }).ToList();

        while (true)
        {
            foreach (var vm in vms)
            {
                while (vm.Output.Any())
                {
                    var destination = (int)vm.Result;
                    var x           = vm.Result;
                    var y           = vm.Result;

                    if (destination == 255)
                    {
                        return($"{y}");
                    }

                    vms[destination].Run(x, y);
                }

                vm.Run(-1);
            }
        }
    }
Пример #4
0
        public override string PartTwo(string input)
        {
            var vm = new IntCodeVM(input)
            {
                InputFunction  = Input,
                OutputFunction = Output
            };

            vm.Run();

            _map = _mapString.CreateCharGrid();
            Log(_map.GetString());

            // figured these out manually on paper
            var routine = "A,C,A,C,B,A,B,A,B,C\n";
            var a       = "R,12,L,8,L,4,L,4\n";
            var b       = "L,8,L,4,R,12,L,6,L,4\n";
            var c       = "L,8,R,6,L,6\n";
            var vid     = "n\n";

            _input = routine + a + b + c + vid;

            vm.Reset();
            vm.SetMemory(0, 2);
            vm.Run();

            return(_spaceDust.ToString());
        }
Пример #5
0
 public Day7()
 {
     for (var i = 0; i < 5; i++)
     {
         Amplifiers[i] = new IntCodeVM(Input.First());
     }
 }
Пример #6
0
        private bool CheckBeamCoords(int x, int y, IntCodeVM vm)
        {
            vm.Reset();
            vm.AddInput(x);
            vm.AddInput(y);

            return(vm.Run()[0] > 0);
        }
Пример #7
0
        public override string PartOne(string input)
        {
            var vm = new IntCodeVM(input);

            vm.SetMemory(1, 12);
            vm.SetMemory(2, 2);

            return(vm.Run().ToString());
        }
            public void Examples(long[] program, long[] expectedOutput)
            {
                var vm      = new IntCodeVM(program);
                var process = new Process(vm);

                process.Run();

                Assert.Equal(expectedOutput, vm.Memory);
            }
Пример #9
0
            public void Examples(long[] program, int input, int expectedOutput)
            {
                var vm = new IntCodeVM(program).AddInputs(input);

                var process = new Process(vm);

                process.Run();

                Assert.Equal(expectedOutput, vm.Output.Peek());
            }
Пример #10
0
            public void Examples(long[] program, long[] expectedProgramState, params int[] inputs)
            {
                var vm = new IntCodeVM(program).AddInputs(inputs);

                var process = new Process(vm);

                process.Run();

                Assert.Equal(expectedProgramState, vm.Memory);
            }
Пример #11
0
    public override string Part2()
    {
        var vms = Enumerable.Range(0, 50)
                  .Select((_, i) =>
        {
            var vm = new IntCodeVM(Input.First());
            vm.Run(i);
            return(vm);
        }).ToList();

        long natX = 0, natY = 0, lastYSent = -1;

        while (true)
        {
            var numIdle = 0;
            foreach (var vm in vms)
            {
                var isIdle = true;
                while (vm.Output.Any())
                {
                    var destination = (int)vm.Result;
                    var x           = vm.Result;
                    var y           = vm.Result;

                    if (destination == 255)
                    {
                        natX = x;
                        natY = y;
                    }
                    else
                    {
                        vms[destination].Run(x, y);
                    }

                    isIdle = false;
                }

                vm.Run(-1);
                if (isIdle)
                {
                    numIdle++;
                }
            }

            if (numIdle == 50)
            {
                if (natY == lastYSent)
                {
                    return($"{natY}");
                }
                vms[0].Run(natX, natY);
                lastYSent = natY;
            }
        }
    }
Пример #12
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM intCodeVM = new IntCodeVM(program);

            intCodeVM.ResizeMemory(int.MaxValue >> 4);
            intCodeVM.AddInput(2);

            intCodeVM.ExecuteProgram();

            return($"{intCodeVM.GetLastOutput()}");
        }
Пример #13
0
        public override string PartOne(string input)
        {
            _vm = new IntCodeVM(input)
            {
                InputFunction  = GetInput,
                OutputFunction = PaintPanel
            };

            _vm.Run();

            return(_panels.Count.ToString());
        }
Пример #14
0
        private void ExploreMap(string input)
        {
            _vm = new IntCodeVM(input)
            {
                InputFunction  = BotInput,
                OutputFunction = BotOutput
            };

            _open.Add(_bot);

            _vm.Run();
        }
Пример #15
0
        static void Main(string[] args)
        {
            program = FetchInput();

            vm = new IntCodeVM(1, (long[])program.Clone(), new long[] { 0 });
            vm.SpecialInstructions = "PainterBot";
            vm.Debug = true;
            #if DEBUG
            vm.Verbose = true;
            #endif
            vm.Run();

            Console.WriteLine("Output: {0}", vm.Output);
        }
Пример #16
0
        public override string PartOne(string input)
        {
            var vm = new IntCodeVM(input);

            vm.OutputFunction = Output;

            vm.Run();

            _map = _mapString.CreateCharGrid();

            var intersections = _map.GetPoints().Where(p => _map[p.X, p.Y] == '#')
                                .Where(p => _map.GetNeighbors(p.X, p.Y, false)
                                       .All(c => c == '#'))
                                .ToList();

            var result = intersections.Sum(i => i.X * i.Y);

            return(result.ToString());
        }
Пример #17
0
        public override string PartTwo(string input)
        {
            _vm = new IntCodeVM(input)
            {
                InputFunction  = GetInput,
                OutputFunction = PaintPanel
            };

            _panels.Add(_pos, true);

            _vm.Run();

            var width  = _panels.Max(p => p.Key.X) - _panels.Min(p => p.Key.X) + 1;
            var height = _panels.Max(p => p.Key.Y) - _panels.Min(p => p.Key.Y) + 1;

            ImageHelper.CreateBitmap(width, height, @"C:\AdventOfCode\Day11.bmp", GetPixel);

            return(@"C:\AdventOfCode\Day11.bmp");
        }
Пример #18
0
        private List <(int start, int end)?> MapBeam(int rowCount, string program)
        {
            var vm = new IntCodeVM(program, true);

            var beam = new List <(int left, int right)?>(rowCount);

            beam.AddMany(null, rowCount);

            // these are hardcoded based on inspecting the puzzle input
            var left     = 4;
            var right    = 5;
            var startRow = 6;

            // Treat the first few rows special - they have either 0 or 1
            for (var y = 0; y < startRow; y++)
            {
                for (var x = 0; x <= right; x++)
                {
                    if (CheckBeamCoords(x, y, vm))
                    {
                        beam[y] = (x, x);
                    }
                }
            }

            for (var y = startRow; y < rowCount; y++)
            {
                if (!CheckBeamCoords(left, y, vm))
                {
                    left++;
                }

                if (CheckBeamCoords(right, y, vm))
                {
                    right++;
                }

                beam[y] = (left, right - 1);
            }

            return(beam);
        }
Пример #19
0
        static void Main(string[] args)
        {
            sw      = new Stopwatch();
            program = FetchInput();

            sw.Start();
            vm = new IntCodeVM(1, (long[])program.Clone(), new long[] { 2 });
            #if DEBUG
            vm.Verbose = true;
            #endif
            vm.Run();
            sw.Stop();

            Console.WriteLine("Output: {0}", vm.Output);
            Console.WriteLine("Completed in {0}ms ({1} ticks).", sw.ElapsedMilliseconds, sw.ElapsedTicks);
            Console.WriteLine("loops: {0}", vm.loopCount);

            Console.ReadLine();
            Environment.Exit(0);
        }
Пример #20
0
        public override string PartOne(string input)
        {
            var vm = new IntCodeVM(input)
            {
                InputFunction = Input
            };

            _input = "NOT A J\n" +
                     "NOT B T\n" +
                     "OR T J\n" +
                     "NOT C T\n" +
                     "OR T J\n" +
                     "AND D J\n" +
                     "WALK\n";

            var outputs = vm.Run();

            DisplayOutputs(outputs);

            return(outputs.Last().ToString());
        }
Пример #21
0
        public override string PartTwo(string input)
        {
            var vm = new IntCodeVM(input);

            for (var noun = 0; noun <= 99; noun++)
            {
                for (var verb = 0; verb <= 99; verb++)
                {
                    vm.SetMemory(1, noun);
                    vm.SetMemory(2, verb);

                    if (vm.Run() == 19690720)
                    {
                        return((100 * noun + verb).ToString());
                    }

                    vm.Reset();
                }
            }

            throw new Exception();
        }
Пример #22
0
 public Day11()
 {
     vm = new IntCodeVM(Input.First());
 }
Пример #23
0
 public Day21() : base(21, "Springdroid Adventure")
 {
     vm = new(Input.First());
 }
Пример #24
0
        /// <inheritdoc />
        public string Part2()
        {
            IntCodeVM intCodeVM = new IntCodeVM(program);

            // get all possible combinations (not very efficient)
            int searchSpaceTop    = 9;
            int searchSpaceBottom = 5;
            int highestOutput     = 0;

            for (int i = searchSpaceBottom; i <= searchSpaceTop; i++)
            {
                for (int j = searchSpaceBottom; j <= searchSpaceTop; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }

                    for (int k = searchSpaceBottom; k <= searchSpaceTop; k++)
                    {
                        if (k == i || k == j)
                        {
                            continue;
                        }

                        for (int l = searchSpaceBottom; l <= searchSpaceTop; l++)
                        {
                            if (l == i || l == j || l == k)
                            {
                                continue;
                            }

                            for (int m = searchSpaceBottom; m <= searchSpaceTop; m++)
                            {
                                if (m == i || m == j || m == k || m == l)
                                {
                                    continue;
                                }

                                IntCodeVM firstVM = new IntCodeVM(program);
                                firstVM.AddInput(i);

                                IntCodeVM secondVM = new IntCodeVM(program);
                                secondVM.AddInput(j);

                                IntCodeVM thirdVM = new IntCodeVM(program);
                                thirdVM.AddInput(k);

                                IntCodeVM fourthVM = new IntCodeVM(program);
                                fourthVM.AddInput(l);

                                IntCodeVM fifthVM = new IntCodeVM(program);
                                fifthVM.AddInput(m);

                                bool isRunning  = true;
                                int  firstInput = 0;

                                // keep running till last VM finishes
                                while (isRunning)
                                {
                                    firstVM.AddInput(firstInput);
                                    firstVM.ResumeProgram();
                                    int firstOutput = (int)firstVM.GetLastOutput();

                                    secondVM.AddInput(firstOutput);
                                    secondVM.ResumeProgram();
                                    int secondOutput = (int)secondVM.GetLastOutput();

                                    thirdVM.AddInput(secondOutput);
                                    thirdVM.ResumeProgram();
                                    int thirdOutput = (int)thirdVM.GetLastOutput();

                                    fourthVM.AddInput(thirdOutput);
                                    fourthVM.ResumeProgram();
                                    int fourthOutput = (int)fourthVM.GetLastOutput();

                                    fifthVM.AddInput(fourthOutput);
                                    isRunning  = (fifthVM.ResumeProgram() == IntCodeVM.HaltCode.Finished);
                                    firstInput = (int)fifthVM.GetLastOutput();
                                }

                                if (firstInput > highestOutput)
                                {
                                    highestOutput = firstInput;
                                    Console.WriteLine($"New highest: {highestOutput} at {i} {j} {k} {l} {m}");
                                }
                            }
                        }
                    }
                }
            }

            return($"{highestOutput}");

            //3909195 is too low
        }
Пример #25
0
 public Day15() : base(15, "Oxygen System")
 {
     vm = new(Input.First());
 }
Пример #26
0
 public Day09() : base(9, "Sensor Boost")
 {
     vm = new(Input.First());
 }
Пример #27
0
        /// <inheritdoc />
        public string Part1()
        {
            IntCodeVM intCodeVM;

            // get all possible combinations (not very efficient)
            int searchSpace   = 4;
            int highestOutput = 0;

            for (int i = 0; i <= searchSpace; i++)
            {
                intCodeVM = new IntCodeVM(program);
                intCodeVM.AddInput(i);
                intCodeVM.AddInput(0);
                intCodeVM.ExecuteProgram();
                int firstOutput = (int)intCodeVM.GetOutputs()[0];

                for (int j = 0; j <= searchSpace; j++)
                {
                    if (j == i)
                    {
                        continue;
                    }

                    intCodeVM = new IntCodeVM(program);
                    intCodeVM.AddInput(j);
                    intCodeVM.AddInput(firstOutput);
                    intCodeVM.ExecuteProgram();
                    int secondOutput = (int)intCodeVM.GetOutputs()[0];

                    for (int k = 0; k <= searchSpace; k++)
                    {
                        if (k == i || k == j)
                        {
                            continue;
                        }

                        intCodeVM = new IntCodeVM(program);
                        intCodeVM.AddInput(k);
                        intCodeVM.AddInput(secondOutput);
                        intCodeVM.ExecuteProgram();
                        int thirdOutput = (int)intCodeVM.GetOutputs()[0];;

                        for (int l = 0; l <= searchSpace; l++)
                        {
                            if (l == i || l == j || l == k)
                            {
                                continue;
                            }

                            intCodeVM = new IntCodeVM(program);
                            intCodeVM.AddInput(l);
                            intCodeVM.AddInput(thirdOutput);
                            intCodeVM.ExecuteProgram();
                            int fourthOutput = (int)intCodeVM.GetOutputs()[0];

                            for (int m = 0; m <= searchSpace; m++)
                            {
                                if (m == i || m == j || m == k || m == l)
                                {
                                    continue;
                                }

                                intCodeVM = new IntCodeVM(program);
                                intCodeVM.AddInput(m);
                                intCodeVM.AddInput(fourthOutput);
                                intCodeVM.ExecuteProgram();
                                int output = (int)intCodeVM.GetOutputs()[0];

                                if (output > highestOutput)
                                {
                                    highestOutput = output;
                                    Console.WriteLine($"New highest: {highestOutput} at {i} {j} {k} {l} {m}");
                                }
                            }
                        }
                    }
                }
            }

            return($"{highestOutput}");
        }
Пример #28
0
 public Day19() : base(19, "Tractor Beam")
 {
     vm   = new(Input.First());
     grid = new long[50, 50];
 }
Пример #29
0
        public override string PartTwo(string input)
        {
            var vm = new IntCodeVM(input);

            return(vm.Run(5).Last().ToString());
        }
Пример #30
0
 public Day11() : base(11, "Space Police")
 {
     vm = new(Input.First());
 }