コード例 #1
0
ファイル: SolverTests.cs プロジェクト: LordPall/Advent2019
        public void TestAmplifierPhaseSet(int phaseVal, int[] expectedPhase)
        {
            string     testProgram = "3,8,1001,8,10,8,105,1,0,0,21,30,51,76,101,118,199,280,361,442,99999,3,9,102,5,9,9,4,9,99,3,9,102,4,9,9,1001,9,3,9,102,2,9,9,101,2,9,9,4,9,99,3,9,1002,9,3,9,1001,9,4,9,102,5,9,9,101,3,9,9,1002,9,3,9,4,9,99,3,9,101,5,9,9,102,4,9,9,1001,9,3,9,1002,9,2,9,101,4,9,9,4,9,99,3,9,1002,9,2,9,1001,9,3,9,102,5,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,99";
            Amplifiers curAmps     = new Amplifiers(5, testProgram);

            curAmps.SetPhaseSettings(phaseVal);
            for (int intI = 0; intI < 5; intI++)
            {
                Assert.Equal(curAmps.GetPhase(intI), expectedPhase[intI]);
            }
        }
コード例 #2
0
ファイル: Solver.cs プロジェクト: LordPall/Advent2019
        public void SolveDaySeven()
        {
            string resultsFile = "adventDaySevenSolution.txt";
            string dataFile    = "adventDaySeven.txt";
            string outFile     = Path.Combine(baseDir, resultsFile);
            string inFile      = Path.Combine(baseDir, dataFile);

            string[]   sourceData = ReadAllLines(inFile);
            long       curMax     = long.MinValue;
            Amplifiers curAmps    = new Amplifiers(5, sourceData[0]);


            // base 5 number  ?
            long nextVal = 0;

            List <int>    phasesRun        = new List <int>();
            List <long>   results          = new List <long>();
            List <string> generatedNumbers = new List <string>();

            char[] digits    = new char[] { '5', '6', '7', '8', '9' };
            int    numDigits = 5;
            int    maxPhase  = 0;

            Solver.GeneratePermutations(digits, numDigits, "", numDigits, ref generatedNumbers);

            StreamWriter sw = new StreamWriter(outFile);


            foreach (string curNumString in generatedNumbers)
            {
                if (HasDuplicateChars(curNumString))
                {
                    continue;
                }
                int curPhase = int.Parse(curNumString);
                curAmps.ResetAmplifiers();
                curAmps.SetPhaseSettings(curPhase);
                phasesRun.Add(curPhase);
                sw.WriteLine("BEGINNING AMP RUN FOR PHASE " + curPhase);
                nextVal = curAmps.RunAmplifiers(sw);
                results.Add(nextVal);
                if (nextVal > curMax)
                {
                    curMax   = nextVal;
                    maxPhase = curPhase;
                }
                sw.WriteLine("--------------END RUN END RUN------------");
            }
            sw.WriteLine("FOR " + maxPhase + " value is " + curMax);
            sw.Close();
        }