예제 #1
0
 public override Registers Invoke(Instruction instr, Registers input) =>
 input.SetReg(instr.C, instr.A);
예제 #2
0
 public override Registers Invoke(Instruction instr, Registers input) =>
 input.SetReg(instr.C, input[instr.A] == input[instr.B] ? 1 : 0);
예제 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Day 19");
            Console.WriteLine("Star 1");
            Console.WriteLine("");

            List <Operation> operations = new List <Operation>()
            {
                new ADDR(),
                new ADDI(),
                new MULR(),
                new MULI(),
                new BANR(),
                new BANI(),
                new BORR(),
                new BORI(),
                new SETR(),
                new SETI(),
                new GTIR(),
                new GTRI(),
                new GTRR(),
                new EQIR(),
                new EQRI(),
                new EQRR()
            };

            foreach (Operation operation in operations)
            {
                opDict.Add(operation.Op, operation);
            }

            instructions = new List <Instruction>();

            int boundRegister = -1;

            foreach (string line in System.IO.File.ReadAllLines(inputFile))
            {
                if (line[0] == '#')
                {
                    boundRegister = int.Parse(line.Substring(4));
                    continue;
                }

                instructions.Add(new Instruction(line));
            }

            Registers reg = new Registers(0, 0, 0, 0, 0, 0);

            while (true)
            {
                instrPtr = reg[boundRegister];

                //Halt when instrPtr exceeds instructions
                if (instrPtr >= instructions.Count)
                {
                    break;
                }

                //Execute instruction
                reg = instructions[instrPtr].Execute(reg);

                //Increment Instr pointer
                reg = reg.SetReg(boundRegister, reg[boundRegister] + 1);
            }

            Console.WriteLine($"Register 0 At End: [{reg.A}]");
            Console.WriteLine("");
            Console.WriteLine("Star 2");
            Console.WriteLine("");
            Console.WriteLine("");

            Console.WriteLine("Printing out clearer instructions");
            for (instrPtr = 0; instrPtr < instructions.Count; instrPtr++)
            {
                instructions[instrPtr].Print();
            }

            Console.WriteLine("");
            Console.WriteLine("");

            instrPtr = 0;
            reg      = new Registers(1, 0, 0, 0, 0, 0);

            for (int i = 0; i < 1000; i++)
            {
                instrPtr = reg[boundRegister];

                //Halt when instrPtr exceeds instructions
                if (instrPtr >= instructions.Count)
                {
                    break;
                }

                Console.Write(reg);
                //Execute instruction
                reg = instructions[instrPtr].Execute(reg, true);

                //Increment Instr pointer
                reg = reg.SetReg(boundRegister, reg[boundRegister] + 1);
            }
            Console.WriteLine($"B Reg: {reg.B}");
            Console.WriteLine("");

            //This program sums the divisors of the number that ends up in Reg 1.
            //In this case, 10,551,292
            int answer = 0;

            for (int i = 1; i <= reg.B; i++)
            {
                if (reg.B % i == 0)
                {
                    Console.WriteLine(i);
                    answer += i;
                }
            }
            Console.WriteLine("");


            Console.WriteLine($"Register 0 at the end: [{answer}]");
            Console.WriteLine("");
            Console.ReadKey();
        }