Пример #1
0
        public void DifferetObjectIsReturnedForDifferentId()
        {
            // Arrange
            Func <string, object> constructingFunc = _ => new object();
            var factory = new UniqueFactory <string, object>(constructingFunc);

            // Act
            var instance1 = factory.GetOrCreateInstance("1");
            var instance2 = factory.GetOrCreateInstance("2");

            // Assert
            Assert.AreNotEqual(instance1, instance2);
        }
Пример #2
0
        public void AllCreatedInstancesDoesNotStoreDuplicateObjects()
        {
            // Arrange
            Func <string, object> constructingFunc = _ => new object();
            var factory = new UniqueFactory <string, object>(constructingFunc);

            // Act
            var instance1 = factory.GetOrCreateInstance("1");
            var instance2 = factory.GetOrCreateInstance("1");
            var instance3 = factory.GetOrCreateInstance("3");

            // Assert
            Assert.AreEqual(factory.AllCreatedInstances.Count, 2);
        }
Пример #3
0
        public void CreatedInstanceExistsInAllCreatedInstances()
        {
            // Arrange
            Func <string, object> constructingFunc = _ => new object();
            var factory = new UniqueFactory <string, object>(constructingFunc);

            // Act
            var instance1 = factory.GetOrCreateInstance("1");
            var instance2 = factory.GetOrCreateInstance("3");

            // Assert
            Assert.IsTrue(factory.AllCreatedInstances.Contains(instance1));
            Assert.IsTrue(factory.AllCreatedInstances.Contains(instance2));
        }
Пример #4
0
        public void ConstructingFuncIsCalledOnceForSameId()
        {
            // Arrange
            int constructingFuncCount = 0;
            Func <string, object> constructingFunc = _ => { constructingFuncCount++; return(new object()); };
            var factory = new UniqueFactory <string, object>(constructingFunc);

            // Act
            var instance1 = factory.GetOrCreateInstance("1");
            var instance2 = factory.GetOrCreateInstance("1");

            // Assert
            Assert.IsNotNull(instance1);
            Assert.IsNotNull(instance2);
            Assert.AreEqual(constructingFuncCount, 1);
        }
Пример #5
0
        public void CallsConstructingFuncToGetOrCreateInstance()
        {
            // Arrange
            bool hasCalledConstructingFunc      = false;
            Func <string, int> constructingFunc = _ => { hasCalledConstructingFunc = true; return(1); };
            var factory = new UniqueFactory <string, int>(constructingFunc);

            // Act
            var instance = factory.GetOrCreateInstance("1");

            // Assert
            Assert.IsTrue(hasCalledConstructingFunc);
        }
Пример #6
0
        static void Main(string[] args)
        {
            using var inputProvider = new InputProvider <Bag?>("Input.txt", PraseBag);

            var inputs = inputProvider.Where(w => w != null).Cast <Bag>().ToList();

            var santasBag = bagFactory.GetOrCreateInstance("shiny gold bag");

            int topLevelBags = inputs.Count(w => w.CanContainBag(santasBag));

            Console.WriteLine($"Part 1: Bag can be contained by {topLevelBags} top level bags");

            int bagCount = santasBag.ContainedBagsCount;

            Console.WriteLine($"Part 2: Santas bag must contain {bagCount} bags");
        }
Пример #7
0
        private static int SimulateState(List <string> input)
        {
            grid = new UniqueFactory <string, Cell>(coordinates => new Cell(coordinates));

            int y = 0;

            foreach (var row in input)
            {
                for (int x = 0; x < row.Length; x++)
                {
                    var key = Cell.FormatCoordinates(x, y, 0, 0);

                    var cell = grid.GetOrCreateInstance(key);

                    cell.Active = row[x] == '#';
                }

                y++;
            }

            for (int generation = 0; generation < 6; generation++)
            {
                foreach (var cell in grid.AllCreatedInstances)
                {
                    cell.EnsureNeighborsExist();
                }

                foreach (var cell in grid.AllCreatedInstances)
                {
                    cell.Evaluate();
                }

                foreach (var cell in grid.AllCreatedInstances)
                {
                    cell.Update();
                }
            }

            var activeCount = grid.AllCreatedInstances.Count(w => w.Active);

            return(activeCount);
        }