Exemplo n.º 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());
        }
Exemplo n.º 2
0
 void DropAll(LCVM vm)
 {
     foreach (var item in items)
     {
         var command = "drop " + item + '\n';
         vm.AddInputs(command.ToCharArray().Select(c => (long)c));
         while (vm.GetCodeResult() != LCVM.StopCode.Input)
         {
         }
         string text = vm.GetTextOutput();
         //Console.WriteLine(text);
     }
 }
Exemplo n.º 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());
        }
Exemplo n.º 4
0
        public override string Part1()
        {
            codeList = File.ReadAllText("Input/23.txt").Split(',').Select(s => long.Parse(s));

            var outputs = new Dictionary <long, List <long> >();
            var vms     = new Dictionary <long, LCVM>();

            for (int i = 0; i < 50; i++)
            {
                vms[i] = new LCVM(codeList.ToArray());
                vms[i].AddInput(i);
                outputs[i] = new List <long>();
            }

            while (true)
            {
                for (int i = 0; i < 50; i++)
                {
                    LCVM.StopCode result = vms[i].GetCodeResult();
                    if (result == LCVM.StopCode.Input)
                    {
                        vms[i].AddInput(-1);
                    }
                    else if (result == LCVM.StopCode.Output)
                    {
                        while (vms[i].HasOutput())
                        {
                            outputs[i].Add(vms[i].GetOutput());

                            if (outputs[i].Count == 3)
                            {
                                long addr = outputs[i][0];
                                long X    = outputs[i][1];
                                long Y    = outputs[i][2];
                                outputs[i].Clear();

                                if (addr == 255)
                                {
                                    return(Y.ToString());
                                }

                                vms[addr].AddInput(X);
                                vms[addr].AddInput(Y);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        void GetToSecurityPanel(LCVM vm)
        {
            string[] commands =
            {
                "north",
                "take mouse",
                "north",
                "take pointer",
                "south",
                "south",
                "west",
                "take monolith",
                "north",
                "west",
                "take food ration",
                "south",
                "take space law space brochure",
                "north",
                "east",
                "south",
                "south",
                "south",
                "west",
                "take asterisk",
                "south",
                "take mutex",
                "north",
                "east",
                "north",
                "north",
                "east",
                "south",
                "south",
                "west",
                "south",
            };

            foreach (string command in commands)
            {
                vm.AddInputs(command.ToCharArray().Select(c => (long)c).Append('\n'));
                while (vm.GetCodeResult() != LCVM.StopCode.Input)
                {
                }
                string text = vm.GetTextOutput();
                Console.WriteLine(text);
            }
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 7
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());
        }
Exemplo n.º 8
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());
        }
Exemplo n.º 9
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());
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public override string Part2()
        {
            var outputs = new Dictionary <long, List <long> >();
            var vms     = new Dictionary <long, LCVM>();

            for (int i = 0; i < 50; i++)
            {
                vms[i] = new LCVM(codeList.ToArray());
                vms[i].AddInput(i);
                outputs[i] = new List <long>();
            }

            (long X, long Y)NAT    = (0, 0);
            (long X, long Y)oldNAT = (0, 0);

            while (true)
            {
                bool idle = true;
                for (int i = 0; i < 50; i++)
                {
                    LCVM.StopCode result = vms[i].GetCodeResult();
                    if (result == LCVM.StopCode.Input)
                    {
                        vms[i].AddInput(-1);
                    }
                    else if (result == LCVM.StopCode.Output)
                    {
                        while (vms[i].HasOutput())
                        {
                            idle = false;
                            outputs[i].Add(vms[i].GetOutput());

                            if (outputs[i].Count == 3)
                            {
                                long addr = outputs[i][0];
                                long X    = outputs[i][1];
                                long Y    = outputs[i][2];
                                outputs[i].Clear();

                                if (addr == 255)
                                {
                                    oldNAT = NAT;
                                    NAT    = (X, Y);
                                }
                                else
                                {
                                    vms[addr].AddInput(X);
                                    vms[addr].AddInput(Y);
                                }
                            }
                        }
                    }
                }

                if (idle)
                {
                    if (NAT != (0, 0))
                    {
                        vms[0].AddInput(NAT.X);
                        vms[0].AddInput(NAT.Y);

                        if (oldNAT == NAT)
                        {
                            return(NAT.Y.ToString());
                        }
                    }
                }
            }
        }
Exemplo n.º 12
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;
                    }
                }
            }
        }
Exemplo n.º 13
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];
Exemplo n.º 14
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());
        }