コード例 #1
0
        private static void RunOpcodeProgram(long[] originalRegister)
        {
            var register = (long[])originalRegister.Clone();
            var pos      = 0L;
            var running  = true;

            do
            {
                var opcode = register[pos].ToString().PadLeft(6, '0');
                var val    = new ValueRetriever(register, opcode, pos);

                switch (opcode)
                {
                case string o when o.EndsWith("01"):     //addition
                    var a = val.Get(1);

                    var b      = val.Get(2);
                    var addRes = a + b;
                    register[register[pos + 3]] = addRes;
                    pos += 4;
                    break;

                case string o when o.EndsWith("02"):     //multiplication
                    var x = val.Get(1);

                    var y        = val.Get(2);
                    var multiRes = x * y;
                    register[register[pos + 3]] = multiRes;
                    pos += 4;
                    break;

                case string o when o.EndsWith("03"):     //input
                    Console.Write("Input: ");

                    var input = int.Parse(Console.ReadLine() ?? String.Empty);
                    register[register[pos + 1]] = input;
                    pos += 2;
                    break;

                case string o when o.EndsWith("04"):     //output
                    var output = val.Get(1);

                    Console.Write("\r\nOut: " + output);
                    pos += 2;
                    break;

                case string o when o.EndsWith("05"):     //jump if true
                    var jumpIfTrue = val.Get(1);

                    if (jumpIfTrue != 0)
                    {
                        pos = val.Get(2);
                    }
                    else
                    {
                        pos += 3;
                    }
                    break;

                case string o when o.EndsWith("06"):     //jump if false
                    var jumpIfFalse = val.Get(1);

                    if (jumpIfFalse == 0)
                    {
                        pos = val.Get(2);
                    }
                    else
                    {
                        pos += 3;
                    }
                    break;

                case string o when o.EndsWith("07"):     //less then
                    register[register[pos + 3]] =
                        val.Get(1) < val.Get(2)
                            ? 1
                            : 0;

                    pos += 4;
                    break;

                case string o when o.EndsWith("08"):     //equals
                    register[register[pos + 3]] =
                        val.Get(1) == val.Get(2)
                            ? 1
                            : 0;

                    pos += 4;
                    break;

                case string o when o.EndsWith("99"):     //halt
                    Console.WriteLine("\r\nHALT!");

                    running = false;
                    break;

                default:
                    running = false;
                    Console.WriteLine($"ERROR: pos {pos} opcode {opcode}");
                    return;
                }
            } while (running);
        }
コード例 #2
0
        private static long RunOpcodeProgram(State state, long inputSignal)
        {
            var register  = state.Register;
            var latestOut = 0L;

            do
            {
                var opcode = register[state.Pos].ToString().PadLeft(6, '0');
                var val    = new ValueRetriever(register, opcode, state.Pos);

                switch (opcode)
                {
                case string o when o.EndsWith("01"):     //addition
                    var a = val.Get(1);

                    var b      = val.Get(2);
                    var addRes = a + b;
                    register[register[state.Pos + 3]] = addRes;
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("02"):     //multiplication
                    var x = val.Get(1);

                    var y        = val.Get(2);
                    var multiRes = x * y;
                    register[register[state.Pos + 3]] = multiRes;
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("03"):     //input
                    var input = state.SentPhase ? inputSignal : state.Phase;

                    state.SentPhase = true;

                    if (isDebug)
                    {
                        Console.WriteLine("Input: " + input);
                    }
                    register[register[state.Pos + 1]] = input;
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("04"):     //output
                    var output = val.Get(1);

                    if (isDebug)
                    {
                        Console.WriteLine("\r\nOut: " + output);
                    }
                    latestOut  = output;
                    state.Pos += 2;
                    return(output);

                case string o when o.EndsWith("05"):     //jump if true
                    var jumpIfTrue = val.Get(1);

                    if (jumpIfTrue != 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("06"):     //jump if false
                    var jumpIfFalse = val.Get(1);

                    if (jumpIfFalse == 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("07"):     //less then
                    register[register[state.Pos + 3]] =
                        val.Get(1) < val.Get(2)
                            ? 1
                            : 0;

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("08"):     //equals
                    register[register[state.Pos + 3]] =
                        val.Get(1) == val.Get(2)
                            ? 1
                            : 0;

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("99"):     //halt
                    if (isDebug)
                    {
                        Console.WriteLine("\r\nHALT!");
                    }
                    state.IsRunning = false;

                    break;

                default:
                    state.IsRunning = false;
                    Console.WriteLine($"ERROR: pos {state.Pos} opcode {opcode}");
                    return(-1);
                }
            } while (state.IsRunning);

            //unchanged input
            return(inputSignal);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: keydon/adventOfCode
        private static long RunOpcodeProgram(State state, long inputSignal)
        {
            var register  = state.Register;
            var latestOut = 0L;

            do
            {
                var opcode = register[state.Pos].ToString().PadLeft(6, '0');
                var val    = new ValueRetriever(state, opcode);
                //Console.WriteLine($"Current Pos: {state.Pos}");

                switch (opcode)
                {
                case string o when o.EndsWith("01"):     //addition
                    var a = val.Get(1);

                    var b      = val.Get(2);
                    var addRes = a + b;

                    //if (isDebug) Console.WriteLine($"Adding {a} + {b}: " + addRes);
                    val.Set(3, addRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("02"):     //multiplication
                    var x = val.Get(1);

                    var y        = val.Get(2);
                    var multiRes = x * y;
                    //if (isDebug) Console.WriteLine($"Multi {x} + {y}: " + multiRes);
                    val.Set(3, multiRes);
                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("03"):     //input
                    var input = state.SentPhase ? inputSignal : state.Phase;

                    state.SentPhase = true;

                    if (isDebug)
                    {
                        Console.WriteLine("Input: " + input);
                    }
                    val.Set(1, input);
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("04"):     //output
                    var output = val.Get(1);

                    if (isDebug)
                    {
                        Console.WriteLine("\r\nOut: " + output);
                    }
                    latestOut  = output;
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("05"):     //jump if true
                    var jumpIfTrue = val.Get(1);

                    if (jumpIfTrue != 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("06"):     //jump if false
                    var jumpIfFalse = val.Get(1);

                    if (jumpIfFalse == 0)
                    {
                        state.Pos = val.Get(2);
                    }
                    else
                    {
                        state.Pos += 3;
                    }
                    break;

                case string o when o.EndsWith("07"):     //less then
                    val.Set(3,
                            val.Get(1) < val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("08"):     //equals
                    val.Set(3,
                            val.Get(1) == val.Get(2)
                            ? 1
                            : 0
                            );

                    state.Pos += 4;
                    break;

                case string o when o.EndsWith("09"):     //setting rel base
                    var oldRelativeBase = state.RelativeBase;

                    var relativeBaseChange = val.Get(1);
                    state.RelativeBase = oldRelativeBase + relativeBaseChange;
                    //if (isDebug) Console.WriteLine($"\r\n{oldRelativeBase} + {relativeBaseChange} = {state.RelativeBase}");
                    state.Pos += 2;
                    break;

                case string o when o.EndsWith("99"):     //halt
                    if (isDebug)
                    {
                        Console.WriteLine("\r\nHALT!");
                    }
                    //Console.WriteLine(string.Join(", ", state.Register));
                    state.Running = false;

                    break;

                default:
                    state.Running = false;
                    Console.WriteLine($"ERROR: pos {state.Pos} opcode {opcode}");
                    return(-1);
                }
            } while (state.Running);

            return(latestOut);
        }