public void Day14Part2_TestSolution()
        {
            string[] map = DayDataUtilities.ReadLinesFromFile("day14.txt");
            Assert.NotNull(map);
            Assert.Equal(63, map.Length);

            RefineryNanoFactory sut = new RefineryNanoFactory();

            foreach (string recipe in map)
            {
                sut.ParseRecipe(recipe);
            }
            Assert.Equal(63, sut.Chemicals.Count);
            long fuel    = 0;
            long oreCost = 0;

            while (oreCost < 1000000000000)
            {
                oreCost += sut.OreCostFor("FUEL", 1);
                if (oreCost < 1000000000000)
                {
                    fuel++;
                }
            }
            Assert.Equal(1766154, fuel);
        }
Ejemplo n.º 2
0
        public void Day08Part1_TestSolution()
        {
            string sifblob = DayDataUtilities.ReadBigStringFromFile("day08.txt");

            Assert.Equal(15000, sifblob.Length);
            int layerSize = 25 * 6;

            List <string> layers = new List <string>();

            for (int idx = 0; idx < sifblob.Length; idx += layerSize)
            {
                string layer = sifblob.Substring(idx, layerSize);
                layers.Add(layer);
            }

            int min0Layer = int.MaxValue;
            int onesXtwos = 0;

            for (int iLayer = 0; iLayer < layers.Count; iLayer++)
            {
                char[] chs    = layers[iLayer].ToCharArray();
                int    zeroes = chs.Where(c => c == '0').ToList().Count;
                if (zeroes < min0Layer)
                {
                    min0Layer = zeroes;
                    int ones = chs.Where(c => c == '1').ToList().Count;
                    int twos = chs.Where(c => c == '2').ToList().Count;
                    onesXtwos = ones * twos;
                }
            }

            Assert.Equal(2500, onesXtwos);
        }
Ejemplo n.º 3
0
        public void Day02Part2_TestSolution()
        {
            long expectedOutput = 19690720;

            for (long noun = 0; noun <= 99; noun++)
            {
                for (long verb = 0; verb <= 99; verb++)
                {
                    try
                    {
                        List <long> values = DayDataUtilities.ReadMagicSmokePgmFromFile("day02.txt");

                        var sut = new MagicSmokeComputer();
                        sut.ProgramValues = values;
                        sut.Noun          = noun;
                        sut.Run();

                        if (expectedOutput == sut.ProgramValues[0])
                        {
                            Assert.Equal(6472, 100 * noun + verb);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }
            Debug.WriteLine("Done");
        }
Ejemplo n.º 4
0
        public void Day16Part1_TestSolution()
        {
            string map = DayDataUtilities.ReadBigStringFromFile("day16.txt");

            Assert.NotNull(map);
            List <int> signal = map.ToCharArray().Select(c => int.Parse(c.ToString())).ToList();

            Assert.Equal(650, signal.Count);
        }
Ejemplo n.º 5
0
        public void Day15Part1_TestSolution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day15.txt");

            Assert.NotNull(pgm);
            OxyRepairDroid sut = new OxyRepairDroid(pgm);
            Point          pos = sut.FindOxygenSystem();

            Assert.Equal(0, pos.X);
            Assert.Equal(0, pos.Y);
        }
Ejemplo n.º 6
0
        public void Day11Part1_TestSoulution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day11.txt");

            Assert.NotNull(pgm);
            var sut = new HullPainterRobot(pgm);

            int actual = sut.GoPaint();

            Assert.Equal(2021, actual);
        }
        public void Day09Part2_TestSolution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day09.txt");

            Assert.NotNull(pgm);
            var sut = new MagicSmokeComputer(pgm);

            sut.InputPort = 2;
            sut.Run(MagicSmokeComputer.ProgramMode.Start);
            Assert.Equal(1, sut.OutputQueueSize());
            Assert.Equal(80379, sut.OutputPort());
        }
Ejemplo n.º 8
0
        public void Day10Part1_TestSolution()
        {
            string[] map = DayDataUtilities.ReadLinesFromFile("day10.txt");
            Assert.NotNull(map);
            var sut = new MonitorStation(map);

            Assert.Equal(316, sut.Asteroids.Count);
            var bestStation = sut.FindBestStation();

            Assert.Equal(26, bestStation.X);
            Assert.Equal(28, bestStation.Y);
            Assert.Equal(267, bestStation.Steps0);
        }
Ejemplo n.º 9
0
        public void Day02Part1_TestSolution()
        {
            List <long> values = DayDataUtilities.ReadMagicSmokePgmFromFile("day02.txt");

            Assert.Equal(137, values.Count);

            var sut = new MagicSmokeComputer();

            sut.ProgramValues = values;
            sut.Noun          = 12;
            sut.Verb          = 2;
            sut.Run();

            Assert.Equal(3716250, sut.ProgramValues[0]);
        }
Ejemplo n.º 10
0
        public void Day01Part2_TestSolution()
        {
            string[] masses    = DayDataUtilities.ReadLinesFromFile("day01.txt");
            long     totalFuel = 0;
            long     mass;
            long     fuel;

            foreach (var smass in masses)
            {
                mass       = long.Parse(smass);
                fuel       = FuelCounterUpper.FuelFuelCalculator(mass);
                totalFuel += fuel;
            }
            Assert.Equal(5175499, totalFuel);
        }
Ejemplo n.º 11
0
        public void Day22Part1_TestSolutionByFollowingIndexInReverse()
        {
            string[] cmds = DayDataUtilities.ReadLinesFromFile("day22.txt");
            Assert.NotNull(cmds);
            Assert.Equal(100, cmds.Length);
            SlamShufflerIndex sut = new SlamShufflerIndex(10007, 3324);

            for (int i = cmds.Length - 1; i >= 0; i--)
            {
                sut.ShuffleCommandReverse(cmds[i]);
            }
            long actual = sut.TrackedIndex;

            Assert.Equal(2019, actual);
        }
Ejemplo n.º 12
0
        public void Day22Part1_TestSolutionByFollowingIndex()
        {
            string[] cmds = DayDataUtilities.ReadLinesFromFile("day22.txt");
            Assert.NotNull(cmds);
            Assert.Equal(100, cmds.Length);
            SlamShufflerIndex sut = new SlamShufflerIndex(10007, 2019);

            foreach (string cmd in cmds)
            {
                sut.ShuffleCommand(cmd);
            }

            long actual = sut.TrackedIndex;

            Assert.Equal(3324, actual);
        }
        public void Day13Part1_TestSolution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day13.txt");

            Assert.NotNull(pgm);

            var sut = new MagicSmokeComputer(pgm);

            MagicSmokeComputer.ProgramMode status = MagicSmokeComputer.ProgramMode.Start;
            do
            {
                status = sut.Run(status);
            } while (status != MagicSmokeComputer.ProgramMode.Stop);
            Assert.Equal(MagicSmokeComputer.ProgramMode.Stop, status);

            // analyze the out put queue
            PointEqualityComparer   peqc          = new PointEqualityComparer();
            Dictionary <Point, int> dictGameBoard = new Dictionary <Point, int>(peqc);

            do
            {
                int   x      = (int)sut.OutputPort();
                int   y      = (int)sut.OutputPort();
                int   tileId = (int)sut.OutputPort();
                Point p      = new Point(x, y);
                if (dictGameBoard.ContainsKey(p))
                {
                    dictGameBoard[p] = tileId;
                }
                else
                {
                    dictGameBoard.Add(p, tileId);
                }
            } while (sut.OutputQueueSize() > 0);
            Assert.NotEmpty(dictGameBoard);
            int blocktiles = 0;

            foreach (var tile in dictGameBoard.Values)
            {
                if (tile == 2)
                {
                    blocktiles++;
                }
            }
            //int blockedTiles = dictGameBoard.Values.Select(t => t == 2).ToList().Count();
            Assert.Equal(268, blocktiles);
        }
        public void Day14Part1_TestSolution()
        {
            string[] map = DayDataUtilities.ReadLinesFromFile("day14.txt");
            Assert.NotNull(map);
            Assert.Equal(63, map.Length);

            RefineryNanoFactory sut = new RefineryNanoFactory();

            foreach (string recipe in map)
            {
                sut.ParseRecipe(recipe);
            }
            Assert.Equal(63, sut.Chemicals.Count);
            long oreCost = sut.OreCostFor("FUEL", 1);

            Assert.Equal(1065255, oreCost);
        }
Ejemplo n.º 15
0
        public void Day11Part2_TestSoulution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day11.txt");

            Assert.NotNull(pgm);
            var   sut        = new HullPainterRobot(pgm);
            Point firstWhite = new Point(0, 0);

            sut.HullDict.Add(firstWhite, 1);
            int actual = sut.GoPaint();

            Assert.Equal(249, actual);

            int maxX = sut.HullDict.Keys.Max(p => p.X);
            int minX = sut.HullDict.Keys.Min(p => p.X);
            int maxY = sut.HullDict.Keys.Max(p => p.Y);
            int minY = sut.HullDict.Keys.Min(p => p.Y);

            for (int y = maxY; y >= minY; y--)
            {
                for (int i = 0; i < 4; i++)
                {
                    for (int x = minX; x <= maxX; x++)
                    {
                        Point p = new Point(x, y);
                        if (sut.HullDict.ContainsKey(p))
                        {
                            if (sut.HullDict[p] == 1)
                            {
                                Debug.Write("....");
                            }
                            else
                            {
                                Debug.Write("####");
                            }
                        }
                        else
                        {
                            Debug.Write("####");
                        }
                    }
                    Debug.WriteLine("=");
                }
            }
            Assert.Equal(-5, minY);
        }
Ejemplo n.º 16
0
        public void Day07Part2_TestSolution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day07.txt");

            Assert.NotNull(pgm);
            long minPhase  = 5;
            long maxPhase  = 9;
            long maxOutput = long.MinValue;

            for (long phaseA = minPhase; phaseA <= maxPhase; phaseA++)
            {
                for (long phaseB = minPhase; phaseB <= maxPhase; phaseB++)
                {
                    if (phaseA != phaseB)
                    {
                        for (long phaseC = minPhase; phaseC <= maxPhase; phaseC++)
                        {
                            if (phaseA != phaseC && phaseB != phaseC)
                            {
                                for (long phaseD = minPhase; phaseD <= maxPhase; phaseD++)
                                {
                                    if (phaseA != phaseD && phaseB != phaseD && phaseC != phaseD)
                                    {
                                        for (long phaseE = minPhase; phaseE <= maxPhase; phaseE++)
                                        {
                                            if (phaseA != phaseE && phaseB != phaseE && phaseC != phaseE && phaseD != phaseE)
                                            {
                                                AmplifierRig rig = new AmplifierRig(pgm);

                                                long output = rig.RunFeedbackAmpCircuit(phaseA, phaseB, phaseC, phaseD, phaseE, 0);

                                                if (output > maxOutput)
                                                {
                                                    maxOutput = output;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Assert.Equal(84088865, maxOutput);
        }
Ejemplo n.º 17
0
        public void Day22Part1_TestSolution()
        {
            string[] cmds = DayDataUtilities.ReadLinesFromFile("day22.txt");
            Assert.NotNull(cmds);
            Assert.Equal(100, cmds.Length);
            SlamShuffler sut = new SlamShuffler(10007);

            foreach (string cmd in cmds)
            {
                sut.ShuffleCommand(cmd);
            }

            int actual = sut.DeckOfCards.FindIndex(c => c == 2019);

            Assert.Equal(3324, actual);
            //sut.DumpDeckOfCards(@"c:\work\cards.csv");
        }
        public void Day05Part2_TestSolution()
        {
            List <long> values = DayDataUtilities.ReadMagicSmokePgmFromFile("day05.txt");

            Assert.Equal(223, values[values.Count - 3]);
            Assert.Equal(99, values[values.Count - 2]);
            Assert.Equal(226, values[values.Count - 1]);

            var sut = new MagicSmokeComputer();

            sut.ProgramValues = values;
            sut.InputPort     = 5;
            //sut.InputPort = 1;

            sut.Run();

            Assert.Equal(4655956, sut.OutputPort());
        }
Ejemplo n.º 19
0
        public void Day08Part2_TestSolution2()
        {
            string sifblob = DayDataUtilities.ReadBigStringFromFile("day08.txt");

            Assert.Equal(15000, sifblob.Length);
            int layerSize    = 25 * 6;
            int numberLayers = sifblob.Length / layerSize;

            char[] sifchs = sifblob.ToCharArray();

            char[] sifImage = new char[layerSize];

            for (int iSif = 0; iSif < layerSize; iSif++)
            {
                for (int iLayer = 0; iLayer < numberLayers; iLayer++)
                {
                    int  sifchNow = iSif + (iLayer * layerSize);
                    char ch       = sifchs[sifchNow];
                    if (ch != '2')
                    {
                        sifImage[iSif] = ch;
                        break;
                    }
                }
            }

            for (int i = 0; i < 6; i++)
            {
                for (int k = 0; k < 4; k++)
                {
                    for (int j = 0; j < 25; j++)
                    {
                        for (int l = 0; l < 4; l++)
                        {
                            Debug.Write(sifImage[(i * 25) + j]);
                        }
                    }
                    Debug.WriteLine(" ");
                }
            }

            Assert.Equal('a', 'a');
        }
        public void Day06Part1_TestSolution()
        {
            string[] orbits = DayDataUtilities.ReadLinesFromFile("day06.txt");
            Assert.Equal(1656, orbits.Length);

            NameValueCollection orbitMap = OrbitalMapper.FillOrbitMap(orbits);

            OrbitalMapper.PrintKeysAndValues(orbitMap);
            int totalOrbits = 0;
            int orbitsNow   = 0;

            foreach (var orb in orbitMap.AllKeys)
            {
                orbitsNow    = OrbitalMapper.CountOrbits(orbitMap, orb, 0);
                totalOrbits += orbitsNow;
            }

            Assert.Equal(271151, totalOrbits);
        }
Ejemplo n.º 21
0
        public void Day03Part2_TestSolution()
        {
            string[] vectors = DayDataUtilities.ReadLinesFromFile("day03.txt");

            Assert.Equal(2, vectors.Length);
            List <string> cmds0 = new List <string>(vectors[0].Split(','));
            List <string> cmds1 = new List <string>(vectors[1].Split(','));

            Assert.NotNull(cmds0);
            Assert.NotNull(cmds1);

            PathCmdsToPoints pathMaker = new PathCmdsToPoints();
            List <Point>     path0     = pathMaker.ParsePath(cmds0);
            List <Point>     path1     = pathMaker.ParsePath(cmds1);

            int actualSteps = pathMaker.FindLeastStepsIntersection(path0, path1);

            Assert.Equal(37390, actualSteps);
        }
Ejemplo n.º 22
0
        public void Day10Part2_TestSolution()
        {
            string[] map = DayDataUtilities.ReadLinesFromFile("day10.txt");
            Assert.NotNull(map);
            var sut         = new MonitorStation(map);
            var bestStation = new Point(26, 28);
            var dictLos     = sut.CalculateLOS(bestStation);

            Assert.Equal(256, dictLos.Count);

            var polarPts = sut.MakeMapPolar(bestStation);

            // Start point is angle -1,5707963267948966, ie. -pi/2, wrap angle by adding 2 pi
            foreach (var pt in polarPts)
            {
                if (pt.angle < -1.5707963267948966)
                {
                    pt.angle += 2 * Math.PI;
                }
            }

            Assert.Equal(sut.Asteroids.Count - 1, polarPts.Count);
            SortedDictionary <double, List <PolarPoint> > dictPolar = sut.MakePolarLOS(polarPts);

            sut.DumpPolarLOS(dictPolar);
            Assert.Equal(267, dictPolar.Count);
            int        count   = 1;
            PolarPoint rock200 = null;

            foreach (var angle in dictPolar.Keys)
            {
                if (count == 200)
                {
                    rock200 = dictPolar[angle].Find(r => r.dist == dictPolar[angle].Min(p => p.dist));
                    break;
                }
                count++;
            }
            Assert.NotNull(rock200);
            Assert.Equal(1309, rock200.cartPt.Y + rock200.cartPt.X * 100);
        }
Ejemplo n.º 23
0
        public void Day22Part2_TestSolutionForwardOnce()
        {
            // both are primes, 99% sure.
            long deckSize     = 119315717514047;
            long shuffleTimes = 101741582076661;

            string[] cmds = DayDataUtilities.ReadLinesFromFile("day22.txt");
            Assert.NotNull(cmds);
            Assert.Equal(100, cmds.Length);

            SlamShufflerIndex sut = new SlamShufflerIndex(deckSize, 2020);

            foreach (var cmd in cmds)
            {
                sut.ShuffleCommand(cmd);
            }

            long actual = sut.TrackedIndex;

            Assert.Equal(113106724260569, actual);
        }
Ejemplo n.º 24
0
        public void Day22Part2_TestSolutionJustOnce()
        {
            // both are primes, 99% sure.
            long deckSize     = 119315717514047;
            long shuffleTimes = 101741582076661;

            string[] cmds = DayDataUtilities.ReadLinesFromFile("day22.txt");
            Assert.NotNull(cmds);
            Assert.Equal(100, cmds.Length);

            SlamShufflerIndex sut = new SlamShufflerIndex(deckSize, 2020);

            for (int i = cmds.Length - 1; i >= 0; i--)
            {
                sut.ShuffleCommandReverse(cmds[i]);
            }

            long actual = sut.TrackedIndex;

            Assert.Equal(27321782275977, actual);
        }
        public void Day06Part2_TestSolution()
        {
            string[] orbits = DayDataUtilities.ReadLinesFromFile("day06.txt");
            Assert.Equal(1656, orbits.Length);

            NameValueCollection orbitMap = OrbitalMapper.FillOrbitMap(orbits);

            Assert.Equal(1657, orbitMap.Count);

            List <string> youOrbits = new List <string>();

            youOrbits = OrbitalMapper.GetOrbits(orbitMap, "YOU", youOrbits);
            List <string> santaOrbits = new List <string>();

            santaOrbits = OrbitalMapper.GetOrbits(orbitMap, "SAN", santaOrbits);
            var notInSanta  = santaOrbits.Except(youOrbits).ToList();
            var notInYou    = youOrbits.Except(santaOrbits).ToList();
            int totalOrbits = notInSanta.Count + notInYou.Count;

            Assert.Equal(388, totalOrbits);
        }
        public void Day05Part1_TestSolution()
        {
            List <long> values = DayDataUtilities.ReadMagicSmokePgmFromFile("day05.txt");

            Assert.Equal(223, values[values.Count - 3]);
            Assert.Equal(99, values[values.Count - 2]);
            Assert.Equal(226, values[values.Count - 1]);

            var sut = new MagicSmokeComputer();

            sut.ProgramValues = values;
            sut.InputPort     = 1;

            sut.Run();
            long actualCode = 0;

            do
            {
                actualCode = sut.OutputPort();
            } while (actualCode == 0);
            Assert.Equal(14522484, actualCode);
        }
        public void Day13Part2_TestSolution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day13.txt");

            Assert.NotNull(pgm);

            int   blockedTiles;
            int   val;
            var   sut    = new MagicSmokeComputer(pgm);
            Point pscore = new Point(-1, 0);
            Point ball   = null;
            Point paddle = null;
            PointEqualityComparer   peqc          = new PointEqualityComparer();
            Dictionary <Point, int> dictGameBoard = new Dictionary <Point, int>(peqc);

            sut.Coins = 2;
            MagicSmokeComputer.ProgramMode status = MagicSmokeComputer.ProgramMode.Start;
            do
            {
                status = sut.Run(status);

                // dump out the screen
                while (sut.OutputQueueSize() > 0)
                {
                    int   x      = (int)sut.OutputPort();
                    int   y      = (int)sut.OutputPort();
                    int   tileId = (int)sut.OutputPort();
                    Point p      = new Point(x, y);
                    if (dictGameBoard.ContainsKey(p))
                    {
                        dictGameBoard[p] = tileId;
                    }
                    else
                    {
                        dictGameBoard.Add(p, tileId);
                    }
                }
                // print the score
                //Console.Clear();
                //foreach (Point p in dictGameBoard.Keys)
                //{
                //    if (p.X != -1)
                //    {
                //        WriteAt(p, dictGameBoard[p]);
                //    }
                //}
                blockedTiles = 0;
                foreach (var item in dictGameBoard)
                {
                    if (item.Value == 2)
                    {
                        blockedTiles++;
                    }
                    if (item.Value == 3)
                    {
                        paddle = item.Key;
                    }
                    if (item.Value == 4)
                    {
                        ball = item.Key;
                    }
                }
                //Console.WriteLine();
                //Console.WriteLine();
                //Console.WriteLine("Score: {0}", dictGameBoard[pscore]);
                //Console.WriteLine("Tiles: {0}", blockedTiles);
                //Console.WriteLine();
                // put the input here
                if (ball.X < paddle.X)
                {
                    // left
                    val = -1;
                }
                else if (ball.X > paddle.X)
                {
                    // right
                    val = 1;
                }
                else
                {
                    val = 0;
                }
                sut.InputPort = val;
            } while (status != MagicSmokeComputer.ProgramMode.Stop);

            Assert.Equal(13989, dictGameBoard[pscore]);
        }
Ejemplo n.º 28
0
        public void Day08Part2_TestSolution()
        {
            string sifblob = DayDataUtilities.ReadBigStringFromFile("day08.txt");

            Assert.Equal(15000, sifblob.Length);
            int layerSize = 25 * 6;

            List <string> layers = new List <string>();

            for (int idx = 0; idx < sifblob.Length; idx += layerSize)
            {
                string layer = sifblob.Substring(idx, layerSize);
                layers.Add(layer);
            }

            char[,] sifImage = new char[6, 25];
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 25; j++)
                {
                    sifImage[i, j] = ' ';
                }
            }

            int assignments = 0;

            for (int iLayer = 0; iLayer < layers.Count; iLayer++)
            {
                char[] chs = layers[iLayer].ToCharArray();
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 25; j++)
                    {
                        if (sifImage[i, j] == ' ')
                        {
                            if (chs[i * 25 + j] != '2')
                            {
                                sifImage[i, j] = chs[i * 25 + j];
                                assignments++;
                            }
                        }
                    }
                }
            }

            Assert.Equal(150, assignments);
            for (int i = 0; i < 6; i++)
            {
                for (int l = 0; l < 4; l++)
                {
                    for (int j = 0; j < 25; j++)
                    {
                        for (int k = 0; k < 4; k++)
                        {
                            Debug.Write(sifImage[i, j]);
                        }
                    }
                    Debug.WriteLine(" ");
                }
            }

            Assert.Equal('a', 'a');
        }
        public void Day25Part1_TestSolution()
        {
            List <long> pgm = DayDataUtilities.ReadMagicSmokePgmFromFile("day25.txt");

            Assert.NotNull(pgm);
        }