示例#1
0
 public static MachineStatus RunToInput(MachineStatus st, int singleInput, Action <string, long, long> outputFunc)
 {
     return(Calc(st, new List <long>()
     {
         singleInput
     }, outputFunc, CalcMode.RunToFirstInput));
 }
示例#2
0
 public static MachineStatus RunToInput(MachineStatus st, int singleInput)
 {
     return(Calc(st, new List <long>()
     {
         singleInput
     }, (s, l, arg3) => { }, CalcMode.RunToFirstInput));
 }
示例#3
0
        public static MachineStatus Perform(string cmd, MachineStatus st)
        {
            var scr = new List <long>();

            scr = (cmd + "\n").ToCharArray().Select(c => (long)c).ToList();
            st  = Machine.Calc(st, scr, (a, b, c) => { Console.Write((char)c); });
            return(st);
        }
示例#4
0
        public static void CalcPart1()
        {
            var asm = new long[5500];

            Day25Input.Day25Code.CopyTo(asm, 0);
            var st = new MachineStatus();

            st.memory = asm;
            var scr = new List <long>();

            st = Machine.Calc(st, scr, (a, b, c) => { Console.Write((char)c); });
            st = Perform("west", st);
            st = Perform("take fixed point", st);
            st = Perform("north", st);
            st = Perform("take sand", st);
            st = Perform("south", st);
            st = Perform("east", st);
            st = Perform("east", st);  //observ
            st = Perform("take asterisk", st);
            st = Perform("north", st); //arcade
            st = Perform("north", st); //Passages
            st = Perform("take hypercube", st);
            st = Perform("north", st); //Storage
            st = Perform("take coin", st);
            st = Perform("north", st); //navigation
            st = Perform("take easter egg", st);
            //st = Perform("west", st); //hot choc
            st = Perform("south", st); //Storage
            st = Perform("south", st); //Passages
                                       //st = Perform("west", st); //Holodeck, empty
            st = Perform("south", st); //Arcade
            st = Perform("west", st);  //Crew q
            st = Perform("north", st); //corridor
            st = Perform("take spool of cat6", st);
            st = Perform("north", st); //kitchen
            st = Perform("take shell", st);
            st = Perform("west", st);  //Security checkpoint
            var things = new Dictionary <int, string> {
                { 0, "easter egg" },
                { 1, "sand" },
                { 2, "fixed point" },
                { 3, "coin" },
                { 4, "spool of cat6" },
                { 5, "shell" },
                { 6, "hypercube" },
                { 7, "asterisk" }
            };
            var oldGrayCode = Gray.IntToGray(170);

            for (int i = 0; i < 256; i++)
            {
                var currCode = (i + 19) % 256;
                Console.Write(currCode);
                var grayCode = Gray.IntToGray(currCode);
                Gray.showBitsInMemory(grayCode);
                for (int j = 0; j < 8; j++)
                {
                    if (oldGrayCode[j] == 1 && grayCode[j] == 0)
                    {
                        st = Perform($"drop {things[j]}", st);
                    }
                    ;
                    if (oldGrayCode[j] == 0 && grayCode[j] == 1)
                    {
                        st = Perform($"take {things[j]}", st);
                    }
                }
                st          = Perform("north", st); //Pressure sensitive, failure, back in SEC
                st          = Perform("inv", st);   //corridor
                oldGrayCode = grayCode;
                Console.ReadLine();
                Console.Clear();
            }
        }
示例#5
0
        public static MachineStatus Calc(MachineStatus st, List <long> Inputs, Action <string, long, long> outputFunc, CalcMode calcMode = default)
        {
            while (st.pc < st.memory.Length)
            {
                var opcode   = st.memory[st.pc] % 100;
                var arg1Mode = ArgMode(st.memory[st.pc], 100);
                var arg2Mode = ArgMode(st.memory[st.pc], 1000);
                var arg3Mode = ArgMode(st.memory[st.pc], 10000);
                long getArg1()
                {
                    switch (opcode)
                    {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        var  addr = st.memory[st.pc + 1];
                        long arg  = addr;
                        if (arg1Mode == 0)
                        {
                            arg = st.memory[addr];
                        }
                        if (arg1Mode == 2)
                        {
                            arg = st.memory[addr + st.RelativeBase];
                        }
                        return(arg);
                    }

                    return(0);
                }

                long getArg2()
                {
                    switch (opcode)
                    {
                    case 1:
                    case 2:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                        var  addr = st.memory[st.pc + 2];
                        long arg  = addr;
                        if (arg2Mode == 0)
                        {
                            arg = st.memory[addr];
                        }
                        if (arg2Mode == 2)
                        {
                            arg = st.memory[addr + st.RelativeBase];
                        }
                        return(arg);
                    }

                    return(0);
                }

                long getArg3()
                {
                    switch (opcode)
                    {
                    case 7:
                    case 8:
                        var  addr = st.memory[st.pc + 3];
                        long arg  = addr;

                        if (arg3Mode == 2)
                        {
                            arg = st.memory[addr + st.RelativeBase];
                        }

                        return(arg);
                    }

                    return(0);
                }

                var arg1 = getArg1();
                var arg2 = getArg2();
                var arg3 = getArg3();
                switch (opcode)
                {
                case 1:
                    var addrt = st.memory[st.pc + 3];
                    if (arg3Mode == 2)
                    {
                        addrt = addrt + st.RelativeBase;
                    }
                    st.memory[addrt] = arg1 + arg2;
                    st.pc            = st.pc + 4;
                    break;

                case 2:
                    addrt = st.memory[st.pc + 3];
                    if (arg3Mode == 2)
                    {
                        addrt = addrt + st.RelativeBase;
                    }
                    st.memory[addrt] = arg1 * arg2;
                    st.pc            = st.pc + 4;
                    break;

                case 3:
                    var addr31 = st.memory[st.pc + 1];
                    if (arg1Mode == 2)
                    {
                        addr31 = st.RelativeBase + addr31;
                    }
                    if (Inputs.Count > 0)
                    {
                        st.memory[addr31] = Inputs[0];
                        Inputs.RemoveAt(0);
                        st.pc = st.pc + 2;
                        if (calcMode == CalcMode.RunToFirstInput)
                        {
                            st.ReturnMode = ReturnMode.ProcessedInput;
                            return(st);
                        }
                    }
                    else
                    {
                        st.ReturnMode = ReturnMode.WaitingForInput;
                        return(st);
                    }
                    break;

                case 4:
                    arg1 = getArg1();
                    outputFunc("Output {0} {1}", st.pc, arg1);
                    st.pc = st.pc + 2;
                    if (calcMode == CalcMode.RunToFirstOutput)
                    {
                        st.Result     = arg1;
                        st.ReturnMode = ReturnMode.ProducedOutput;
                    }
                    break;

                case 5:                         //JNZ
                    if (arg1 != 0)
                    {
                        st.pc = arg2;
                    }
                    else
                    {
                        st.pc = st.pc + 3;
                    }
                    break;

                case 6:                         //JZ
                    if (arg1 == 0)
                    {
                        st.pc = arg2;
                    }
                    else
                    {
                        st.pc = st.pc + 3;
                    }
                    break;

                case 7:                         //LT
                    addrt = st.memory[st.pc + 3];
                    if (arg3Mode == 2)
                    {
                        addrt = addrt + st.RelativeBase;
                    }
                    if (arg1 < arg2)
                    {
                        st.memory[addrt] = 1;
                    }
                    else
                    {
                        st.memory[addrt] = 0;
                    }
                    st.pc = st.pc + 4;
                    break;

                case 8:                         //EQ
                    addrt = st.memory[st.pc + 3];
                    if (arg3Mode == 2)
                    {
                        addrt = addrt + st.RelativeBase;
                    }
                    if (arg1 == arg2)
                    {
                        st.memory[addrt] = 1;
                    }
                    else
                    {
                        st.memory[addrt] = 0;
                    }
                    st.pc = st.pc + 4;
                    break;

                case 9:                         //Set Relative Base
                    st.RelativeBase += arg1;
                    st.pc           += 2;
                    break;

                case 99:
                {
                    st.ReturnMode = ReturnMode.RanToHalt;
                    st.Result     = st.memory[0];
                    return(st);
                }
                }
            }
            return(st);
        }
示例#6
0
 public static MachineStatus RunToOutput(MachineStatus st, long[] memory, Action <string, long, long> outputFunc)
 {
     return(Calc(st, new List <long>(), outputFunc, CalcMode.RunToFirstOutput));
 }
示例#7
0
 public static MachineStatus RunToOutput(MachineStatus st, long[] memory)
 {
     return(Calc(st, new List <long>(), (s, l, arg3) => { }, CalcMode.RunToFirstOutput));
 }