Exemple #1
0
        public override string Part1()
        {
            codeList = File.ReadAllText("Input/13.txt").Split(',').Select(s => long.Parse(s));
            LCVM vm = new LCVM(codeList.ToArray());

            List <long> results = new List <long>();

            var result = LCVM.StopCode.None;

            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    results.Add(vm.GetOutput());
                }
            }

            int blocks = 0;

            for (int i = 2; i < results.Count; i += 3)
            {
                if (results[i] == 2)
                {
                    blocks++;
                }
            }

            return(blocks.ToString());
        }
Exemple #2
0
        Dictionary <(int x, int y), int> GetPaint(int input)
        {
            (int x, int y)pos = (0, 0);
            Dictionary <(int x, int y), int> paint = new Dictionary <(int x, int y), int>();
            int face = 0;

            LCVM vm = new LCVM(codeList.ToArray());

            vm.AddInput(input); // First panel is black

            var  result   = LCVM.StopCode.None;
            bool isColour = true;

            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    if (isColour)
                    {
                        paint[pos] = (int)vm.GetOutput();
                    }
                    else
                    {
                        // Dir
                        face += (int)vm.GetOutput() * 2 - 1;
                        var dir = GetDirection(face);
                        pos = (pos.x + dir.x, pos.y + dir.y);
                    }

                    isColour = !isColour;
                }
                else if (result == LCVM.StopCode.Input)
                {
                    paint.TryGetValue(pos, out int panel);
                    vm.AddInput(panel);
                }
            }

            return(paint);
        }
Exemple #3
0
        public override string Part2()
        {
            LCVM vm = new LCVM(codeList.ToArray());

            string[] instructions = new string[]
            {
                "NOT A J",
                "NOT B T",
                "OR T J",
                "NOT C T",
                "OR T J",
                "AND D J",

                "NOT D T",
                "OR E T",
                "OR H T",
                "AND T J",
                "RUN"
            };

            foreach (string s in instructions)
            {
                var chars = s.ToCharArray();
                foreach (var c in chars)
                {
                    vm.AddInput(c);
                }

                vm.AddInput('\n');
            }

            long lastOutput = 0;
            var  result     = LCVM.StopCode.None;

            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    lastOutput = vm.GetOutput();
                    if (lastOutput < 256)
                    {
                        Console.Write((char)lastOutput);
                    }
                }
            }

            return(lastOutput.ToString());
        }
Exemple #4
0
        public override string Part1()
        {
            codeList = File.ReadAllText("Input/21.txt").Split(',').Select(s => long.Parse(s));
            LCVM vm = new LCVM(codeList.ToArray());

            string[] instructions = new string[]
            {
                "NOT A J",
                "NOT B T",
                "OR T J",
                "NOT C T",
                "OR T J",
                "AND D J",
                "WALK"
            };

            foreach (string s in instructions)
            {
                var chars = s.ToCharArray();
                foreach (var c in chars)
                {
                    vm.AddInput(c);
                }

                vm.AddInput('\n');
            }

            long lastOutput = 0;
            var  result     = LCVM.StopCode.None;

            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    lastOutput = vm.GetOutput();
                    if (lastOutput < 256)
                    {
                        Console.Write((char)lastOutput);
                    }
                }
            }

            return(lastOutput.ToString());
        }
Exemple #5
0
        public override string Part2()
        {
            LCVM vm = new LCVM(codeList.ToArray());

            vm.AddInput(2);

            LCVM.StopCode result = LCVM.StopCode.None;
            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    Console.WriteLine(vm.GetOutput());
                }
            }

            return(base.Part2());
        }
Exemple #6
0
        public override string Part1()
        {
            codeList = File.ReadAllText("Input/9.txt").Split(',').Select(s => long.Parse(s));

            LCVM vm = new LCVM(codeList.ToArray());

            vm.AddInput(1);

            LCVM.StopCode result = LCVM.StopCode.None;
            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    Console.WriteLine(vm.GetOutput());
                }
            }

            return(base.Part1());
        }
Exemple #7
0
        public override string Part1()
        {
            codeList = File.ReadAllText("Input/19.txt").Split(',').Select(s => long.Parse(s));

            long count = 0;

            for (int y = 0; y < 50; y++)
            {
                for (int x = 0; x < 50; x++)
                {
                    LCVM vm = new LCVM(codeList.ToArray());
                    vm.AddInput(x);
                    vm.AddInput(y);
                    vm.GetCodeResult();
                    count += vm.GetOutput();
                }
            }

            return(count.ToString());
        }
Exemple #8
0
        public override string Part2()
        {
            var map = new Dictionary <(int x, int y), bool>();

            int  y            = 1;
            int  x            = 1;
            int  minX         = 0;
            int  rowLength    = 1;
            bool foundThisRow = false;
            bool foundStart   = false;

            while (true)
            {
                LCVM vm = new LCVM(codeList.ToArray());
                vm.AddInput(x);
                vm.AddInput(y);
                vm.GetCodeResult();
                bool isBeam = vm.GetOutput() == 1;
                map.Add((x, y), isBeam);

                if (isBeam)
                {
                    if (foundThisRow)
                    {
                        x++;
                    }
                    else
                    {
                        if (rowLength >= 100)
                        {
                            if (map.TryGetValue((x + 99, y - 99), out bool beam) && beam)
                            {
                                return((x * 10000 + (y - 99)).ToString());
                            }
                        }

                        minX = x;
                        x   += rowLength;
                    }

                    foundThisRow = true;
                    foundStart   = true;
                }
                else if (foundThisRow)
                {
                    // After beam
                    rowLength = x - minX;
                    x         = minX;
                    y++;
                    foundThisRow = false;
                }
                else
                {
                    // Before beam
                    x++;

                    if (!foundStart && x > 10)
                    {
                        y++;
                        x = 0;
                    }
                }
            }
        }
Exemple #9
0
        public override string Part2()
        {
            Console.WriteLine("Press a key to start Part 2:");
            Console.ReadKey();

            var codes = codeList.ToArray();

            codes[0] = 2;
            LCVM vm = new LCVM(codes);

            List <long> results = new List <long>();
            Dictionary <(int x, int y), int> screen = new Dictionary <(int x, int y), int>();

            bool allScreen = false;
            int  ballX     = 0;
            int  paddleX   = 0;
            int  count     = 0;

            var result = LCVM.StopCode.None;

            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();

                if (result == LCVM.StopCode.Input)
                {
                    if (!allScreen)
                    {
                        // Get Max X and Y coordinates
                        allScreen = true;
                        foreach (var item in screen)
                        {
                            maxX = Math.Max(maxX, item.Key.x);
                            maxY = Math.Max(maxY, item.Key.y);
                        }
                    }

                    if (++count == 50)
                    {
                        // Visualisation
                        DrawScreen(screen);
                        count = 0;
                    }

                    vm.AddInput(Math.Sign(ballX - paddleX));
                }
                else if (result == LCVM.StopCode.Output)
                {
                    results.Add(vm.GetOutput());
                }

                if (results.Count == 3)
                {
                    if (results[0] == -1 && results[1] == 0)
                    {
                        score = (int)results[2];
                    }
                    else
                    {
                        int x   = (int)results[0];
                        int y   = (int)results[1];
                        int val = (int)results[2];
                        if (val < 0 || val > 4)
                        {
                            throw new Exception("Value out of range!");
                        }

                        screen[(x, y)] = (int)results[2];
Exemple #10
0
        public override string Part1()
        {
            codeList = File.ReadAllText("Input/25.txt").Split(',').Select(s => long.Parse(s));
            LCVM vm = new LCVM(codeList.ToArray());

            var result = LCVM.StopCode.None;

            while (result != LCVM.StopCode.Halt)
            {
                result = vm.GetCodeResult();
                if (result == LCVM.StopCode.Output)
                {
                    long o = vm.GetOutput();
                    Console.Write((char)o);
                }
                else if (result == LCVM.StopCode.Input)
                {
                    var input = Console.ReadLine();
                    if (input == "collect")
                    {
                        GetToSecurityPanel(vm);
                    }
                    else if (input == "hack")
                    {
                        for (int i = 0; i <= 127; i++)
                        {
                            DropAll(vm);

                            string command = "";
                            for (int j = 0; j < 7; j++)
                            {
                                if ((i & (1 << j)) != 0)
                                {
                                    command = "take " + items[j] + '\n';
                                    vm.AddInputs(command.ToCharArray().Select(c => (long)c));
                                    while (vm.GetCodeResult() == LCVM.StopCode.Output)
                                    {
                                    }
                                    vm.GetTextOutput();
                                }
                            }

                            command = "east\n";
                            vm.AddInputs(command.ToCharArray().Select(c => (long)c));
                            while (vm.GetCodeResult() == LCVM.StopCode.Output)
                            {
                            }

                            string text = vm.GetTextOutput();
                            if (!text.Contains("eject"))
                            {
                                Console.WriteLine(text);
                            }
                        }

                        Console.WriteLine("Finished Hacking!: ");
                    }
                    else
                    {
                        input += '\n';
                        vm.AddInputs(input.ToCharArray().Select(c => (long)c));
                    }
                }
            }


            return(base.Part1());
        }