Example #1
0
        protected override void ReadOpCodeData(IntComputer computer)
        {
            long nextMemoryData = computer.ReadNextMemoryAddress();

            destinationParam = new OpCodeParameter(curInstruction.GetParamMode(0), nextMemoryData, computer);
            inputData        = computer.ReadNextInput();
        }
Example #2
0
        public RepairBot(string inData, string outFile)
        {
            string curMapFile = "BotMap.txt";
            string rootPath   = Path.GetDirectoryName(outFile);

            curMapFile         = Path.Combine(rootPath, curMapFile);
            sw                 = new StreamWriter(outFile);
            xSize              = 42;
            ySize              = 42;
            startNode          = new ExploreNode();
            startNode.nodeType = RobotResultCode.MOVE_SUCCESFUL;
            curNode            = startNode;
            curMap             = new ExploreNode[xSize * ySize];
            curPos.x           = xSize / 2;
            curPos.y           = ySize / 2;
            int startIndex = Helpers.GetIndexFromCoordinate(curPos, xSize);

            curMap[startIndex] = startNode;
            curComputer        = new IntComputer();
            curComputer.InitializeMemory(inData);
            curComputer.StartComputer(true);
            RunRobot();

            //DrawMap();
            int endIndex = Helpers.GetIndexFromCoordinate(curPos, xSize);
            int length   = SearchTree(startIndex, endIndex);

            sw.Close();
        }
Example #3
0
        public void SolveDayNine()
        {
            string resultsFile = "adventDayNineSolution.txt";
            string dataFile    = "adventDayNine.txt";
            string outFile     = Path.Combine(baseDir, resultsFile);
            string inFile      = Path.Combine(baseDir, dataFile);

            string[]    sourceData = ReadAllLines(inFile);
            IntComputer curComp    = new IntComputer();

            curComp.InitializeMemory(sourceData[0]);
            curComp.AddInputData(2); // test mode
            //curComp.InitializeMemory(SolverTests.GetSampleData(0));
            curComp.StartComputer(false);
            while (!curComp.IsProgramCompleted())
            {
                curComp.ResumeProgram();
            }
            string       debugData = curComp.GetDebugOutputString();
            StreamWriter sw        = new StreamWriter(outFile);

            sw.WriteLine(debugData);
            sw.Close();
            //long outVal = curComp.ReadOutputData();
        }
Example #4
0
        public void SolveDayTwo()
        {
            string resultsFile = "adventDayTwoSolution.txt";
            string dataFile    = "adventDayTwo.txt";
            string outFile     = Path.Combine(baseDir, resultsFile);
            string inFile      = Path.Combine(baseDir, dataFile);

            string[] sourceData = ReadAllLines(inFile);
            // should be one line
            IntComputer newComp = new IntComputer();

            newComp.InitializeMemoryFromFile(inFile);

            // we need to find the values that give us 19690720 in address 0 after running. Reset each time.
            if (FindNounVerb(newComp))
            {
                newComp.WriteMemoryToFile(outFile);
                long val = 100 * newComp.ReadMemoryAtAddress(1) + newComp.ReadMemoryAtAddress(2);
            }
            else
            {
                Console.WriteLine("FAIL");
            }

            //19690720
        }
Example #5
0
 protected override void ProcessOpCode(IntComputer computer)
 {
     if (firstVal.ReadParamFromMemory() != 0)
     {// just setting memoryPointer so we don't need to use the specialty writy addresses
         computer.SetMemoryPointer(secondVal.ReadParamFromMemory());
     }
 }
Example #6
0
 public ArcadeMachine(string sourceData, string outFile)
 {
     curComputer = new IntComputer();
     curComputer.InitializeMemory(sourceData);
     curComputer.ReplaceMemoryAtAddress(0, 2); // infinite play
     curComputer.StartComputer(true);
     sw = new StreamWriter(outFile);
 }
Example #7
0
        protected override void ProcessOpCode(IntComputer computer)
        {
            // input is forked up
            // you can have a relative address. It's technically a write, but it can be in immediate, position and relative mode.
            int targetAddress = destinationParam.GetMemoryWriteAddressForInputParameter();

            computer.ReplaceMemoryAtAddress(targetAddress, inputData);
        }
Example #8
0
        protected override void ReadOpCodeData(IntComputer computer)
        {
            long nextMemoryData = computer.ReadNextMemoryAddress();

            firstVal = new OpCodeParameter(curInstruction.GetParamMode(0), nextMemoryData, computer);

            nextMemoryData = computer.ReadNextMemoryAddress();
            secondVal      = new OpCodeParameter(curInstruction.GetParamMode(1), nextMemoryData, computer);
        }
Example #9
0
        public void DoAction2()
        {
            var content  = ReadFile();
            var computer = new IntComputer(content, false);

            computer.SetInputValues(2);
            computer.Run();
            Console.WriteLine($"Last output = {computer.LastOutput}");
        }
Example #10
0
        public void DoAction1()
        {
            Console.WriteLine("Reading file");

            var content = ReadFile();

            var possibilities = new List <Amplification>();

            for (int a = 0; a < 5; a++)
            {
                for (int b = 0; b < 5; b++)
                {
                    for (int c = 0; c < 5; c++)
                    {
                        for (int d = 0; d < 5; d++)
                        {
                            for (int e = 0; e < 5; e++)
                            {
                                if (a == b || a == c || a == d || a == e ||
                                    b == c || b == d || b == e ||
                                    c == d || c == e ||
                                    d == e)
                                {
                                    break;
                                }

                                var ampA = new IntComputer(content, false);
                                ampA.SetInputValues(a, 0);
                                ampA.Run();
                                var ampB = new IntComputer(content, false);
                                ampB.SetInputValues(b, ampA.LastOutput);
                                ampB.Run();
                                var ampC = new IntComputer(content, false);
                                ampC.SetInputValues(b, ampB.LastOutput);
                                ampC.Run();
                                var ampD = new IntComputer(content, false);
                                ampD.SetInputValues(b, ampC.LastOutput);
                                ampD.Run();
                                var ampE = new IntComputer(content, false);
                                ampE.SetInputValues(b, ampD.LastOutput);
                                ampE.Run();

                                possibilities.Add(new Amplification {
                                    A = a, B = b, C = c, D = d, E = e, Output = ampE.LastOutput
                                });
                            }
                        }
                    }
                }
            }

            var highestOutput = possibilities.First(x => x.Output == possibilities.Max(y => y.Output));

            Console.WriteLine($"Configuration {highestOutput.A}{highestOutput.B}{highestOutput.C}{highestOutput.D}{highestOutput.E} gives the highest output of {highestOutput.Output}");
        }
Example #11
0
 protected override void ProcessOpCode(IntComputer computer)
 {
     if (firstVal.ReadParamFromMemory() == secondVal.ReadParamFromMemory())
     {
         computer.ReplaceMemoryAtAddress(thirdVal.GetMemoryWriteAddressFromParameter(), 1);
     }
     else
     {
         computer.ReplaceMemoryAtAddress(thirdVal.GetMemoryWriteAddressFromParameter(), 0);
     }
 }
Example #12
0
        public void TestDayNineSampleData(int sampleIndex)
        {
            IntComputer curComp = new IntComputer();

            curComp.InitializeMemory(GetSampleData(sampleIndex));
            curComp.StartComputer(false);
            long outVal         = curComp.ReadOutputData();
            long expectedRetVal = GetExpectedResults(sampleIndex);

            Assert.Equal(outVal, expectedRetVal);
        }
Example #13
0
 public Amplifiers(int ampCount, string sourceProgram)
 {
     numAmplifiers = ampCount;
     for (int intI = 0; intI < ampCount; intI++)
     {
         IntComputer newComp = new IntComputer();
         newComp.curId = numAmplifiers - intI;
         newComp.InitializeMemory(sourceProgram);
         newComp.StartComputer(true);
         generatedAmplifiers.Add(newComp);
     }
 }
Example #14
0
        public void DoAction1()
        {
            var content = ReadFile();

            var intComputer = new IntComputer(content, false);

            intComputer.Memory[1] = 12;
            intComputer.Memory[2] = 2;
            intComputer.Run();

            Console.WriteLine($"Output {intComputer.Memory[0]}");
        }
Example #15
0
        public void DoAction2() //742621
        {
            Console.WriteLine("Reading file");

            var fileReader = new StreamReader("./Input/Day05.txt");
            var content    = fileReader.ReadToEnd();

            var intcodeComputer = new IntComputer(content, false);

            intcodeComputer.SetInputValues(1);
            intcodeComputer.Run();

            Console.WriteLine(intcodeComputer.OutputValues.First(x => x != 0));
        }
Example #16
0
        public void SolveDayFive()
        {
            string resultsFile = "adventDayFiveSolution.txt";
            string dataFile    = "adventDayFive.txt";
            string outFile     = Path.Combine(baseDir, resultsFile);
            string inFile      = Path.Combine(baseDir, dataFile);

            string[] sourceData = ReadAllLines(inFile);
            // should be one line
            IntComputer newComp = new IntComputer();

            newComp.InitializeMemoryFromFile(inFile);
            newComp.AddInputData(5);
            newComp.StartComputer(false);
            newComp.WriteMemoryToFile(outFile);
        }
Example #17
0
        public static OpCode ReadNextOpCode(IntComputer computer)
        {
            // 1002
            //ABCDE
            //01002 = DE = opcode, c = mode for op 1, b mode for op 2 a, mode for op 3

            long opCodeData = computer.ReadNextMemoryAddress();
            OpCodeInstruction newInstruction = new OpCodeInstruction(opCodeData);

            switch (newInstruction.GetInstruction())
            {
            case OpCodeInstruction.OPCODE_TYPES.ADD:
                return(new AddOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.MUL:
                return(new MulOpcode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.EXIT:
                return(new ExitOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.INPUT:
                return(new InputOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.OUTPUT:
                return(new OutputOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.JUMP_IF_TRUE:
                return(new JumpIfTrueOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.JUMP_IF_FALSE:
                return(new JumpIfFalseOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.LESS_THAN:
                return(new LessThanOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.EQUALS:
                return(new EqualsOpCode(newInstruction));

            case OpCodeInstruction.OPCODE_TYPES.ADJUST_RELATIVE_BASE:
                return(new AdjustRelativeBaseOpCode(newInstruction));

            default:
                Console.WriteLine("DER FARK?");
                return(null);
            }
        }
Example #18
0
        public PainterRobot(string sourceInstructions, int width, int height, string debugFile)
        {
            outFile       = debugFile;
            xSize         = width;
            ySize         = height;
            curPos        = new Vector2();
            curPos.x      = xSize / 2;
            curPos.y      = ySize / 2;
            panelsToPaint = new PaintPanelStruct[xSize * ySize];
            int curIndex = Helpers.GetIndexFromCoordinate(curPos, xSize);

            panelsToPaint[curIndex].curColor = PaintColorEnum.WHITE;

            robotComputer = new IntComputer();
            robotComputer.InitializeMemory(sourceInstructions);
            robotComputer.StartComputer(true);
            RunRobot();
        }
Example #19
0
 public bool FindNounVerb(IntComputer newComp)
 {
     for (int intI = 0; intI < 100; intI++)
     {
         for (int intJ = 0; intJ < 100; intJ++)
         {
             newComp.ResetMemory();
             newComp.ReplaceMemoryAtAddress(1, (long)intI);
             newComp.ReplaceMemoryAtAddress(2, (long)intJ);
             newComp.StartComputer(false);
             long result = newComp.ReadMemoryAtAddress(0);
             if (result == 19690720)
             {
                 // ALL DONE
                 return(true);
             }
         }
     }
     return(false);
 }
Example #20
0
        public void DoAction2()
        {
            var content = ReadFile();

            var loopLength = 100;

            for (var a = 0; a < loopLength; a++)
            {
                for (var b = 0; b < loopLength; b++)
                {
                    var intComputer = new IntComputer(content, false);
                    intComputer.Memory[1] = a;
                    intComputer.Memory[2] = b;
                    intComputer.Run();

                    if (intComputer.Memory[0] == 19690720)
                    {
                        Console.WriteLine($"Output {intComputer.Memory[0]} for Noun {a} and Verb {b}");
                        a = loopLength;
                        b = loopLength;
                    }
                }
            }
        }
Example #21
0
 public OpCodeParameter(PARAMETER_MODES newParamMode, long curVal, IntComputer computer)
 {
     paramMode   = newParamMode;
     paramData   = curVal;
     curComputer = computer;
 }
Example #22
0
 protected virtual void ProcessOpCode(IntComputer computer)
 {
 }
Example #23
0
 public void RunOpCode(IntComputer computer)
 {
     ReadOpCodeData(computer);
     ProcessOpCode(computer);
 }
Example #24
0
 protected virtual void ReadOpCodeData(IntComputer computer)
 {
     // nada
 }
Example #25
0
 protected override void ProcessOpCode(IntComputer computer)
 {
     // this isn't writing to a memory address so we just read from memory
     computer.AdjustRelativeBase(sourceData.ReadParamFromMemory());
 }
Example #26
0
 protected override void ProcessOpCode(IntComputer computer)
 {
     result = firstVal.ReadParamFromMemory() + secondVal.ReadParamFromMemory();
     computer.ReplaceMemoryAtAddress(thirdVal.GetMemoryWriteAddressFromParameter(), result);
 }
Example #27
0
 protected override void ReadOpCodeData(IntComputer computer)
 {
 }
Example #28
0
 protected override void ProcessOpCode(IntComputer computer)
 {
     computer.StopProgram();
 }
Example #29
0
 protected override void ProcessOpCode(IntComputer computer)
 {
     computer.WriteOutputData(targetData.ReadParamFromMemory());
     computer.PauseProgram();
 }