示例#1
0
            public bool ApplyRule(ConwayCube currentCube)
            {
                bool currentstat = _currentConwayCubes.Contains(currentCube);

                int activeNeighbors = _currentConwayCubes.Where(c => c.X >= currentCube.X - 1 && c.X <= currentCube.X + 1)
                                      .Where(c => c.Y >= currentCube.Y - 1 && c.Y <= currentCube.Y + 1)
                                      .Where(c => c.Z >= currentCube.Z - 1 && c.Z <= currentCube.Z + 1)
                                      .Where(c => c.X != currentCube.X || c.Y != currentCube.Y || c.Z != currentCube.Z)
                                      .Count();

                if (currentstat == true)
                {
                    if (activeNeighbors == 2 || activeNeighbors == 3)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                if (activeNeighbors == 3)
                {
                    return(true);
                }


                return(false);
            }
示例#2
0
        public static void Simulate(string startFile)
        {
            Console.WriteLine("Initial state:");
            int y = 0;

            foreach (string line in File.ReadLines(startFile))
            {
                int x = 0;
                foreach (char state in line.Trim())
                {
                    new ConwayCube(x, y, 0, 0, state == '#');
                    x++;
                    Console.Write(state == '#' ? 1 : 0);
                }
                Console.WriteLine();
                y++;
            }
            Console.WriteLine(
                "Active cubes: {0}. Total cubes: {1}",
                ConwayCube.CountActiveCubes(),
                ConwayCube.CubeDict.Count
                );
            for (int i = 0; i < 6; i++)
            {
                ConwayCube.Update();
                Console.WriteLine(
                    "Active cubes after {0} cycles: {1}. Total cubes: {2}",
                    i + 1,
                    ConwayCube.CountActiveCubes(),
                    ConwayCube.CubeDict.Count
                    );
            }
            Console.WriteLine("Active cubes: {0}", ConwayCube.CountActiveCubes());
        }
示例#3
0
        public void Test_GetNeighbours3D()
        {
            var cube   = new ConwayCube(new int[] { 0, 0, 0 });
            var neighs = cube.GetNeighbours().ToList();

            int targetCount = (int)Math.Pow(3, cube.Dimensions) - 1;

            Assert.IsTrue(neighs.Count == targetCount);
        }
示例#4
0
        public void Test_GetOffsetsForDimension()
        {
            var dims = new int[] { 2, 3, 4 };

            foreach (var dim in dims)
            {
                var offsets     = ConwayCube.GetOffsets(dim).ToList();
                int targetCount = (int)Math.Pow(3, dim);

                Assert.IsTrue(offsets.Count() == targetCount);
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            var days = new List <IDay>()
            {
                ExpenseReport.LoadFromFile("Day01/ExpenseReport.txt"),
                PasswordValidator.LoadFromFile("Day02/Passwords.txt"),
                Map.LoadFromFile("Day03/Map.txt"),
                PassportValidator.LoadFromFile("Day04/PassportData.txt"),
                BoardingPasses.LoadFromFile("Day05/Seats.txt"),
                Declerations.LoadFromFile("Day06/Declerations.txt"),
                LuggageRules.LoadFromFile("Day07/LuggageRules.txt"),
                GameConsole.LoadFromFile("Day08/BootCode.txt"),
                AdditionSystem.LoadFromFile("Day09/Data.txt"),
                JoltAdapters.LoadFromFile("Day10/JoltAdapters.txt"),
                SeatingLayout.LoadFromFile("Day11/SeatingLayout.txt"),
                Navigation.LoadFromFile("Day12/NavigationInstructions.txt"),
                Buses.LoadFromFile("Day13/Buses.txt"),
                DockingProgram.LoadFromFile("Day14/DockingProgram.txt"),
                MemoryGame.Create("15,12,0,14,3,1"),
                TicketAnalyser.LoadFromFile("Day16/TicketData.txt"),
                ConwayCube.LoadFromFile("Day17/ConwayCube.txt"),
                OperationOrder.LoadFromFile("Day18/Expressions.txt"),
                JurassicJigsaw.LoadFromFile("Day20/Tiles.txt"),
                Combat.LoadFromFile("Day22/Cards.txt")
            };

            var invalidCount = 0;

            foreach (var day in days)
            {
                var part1 = day.Part1();
                var part2 = day.Part2();

                var part1Invalid = !string.IsNullOrWhiteSpace(day.ValidatedPart1) &&
                                   part1 != day.ValidatedPart1;

                var part2Invalid = !string.IsNullOrWhiteSpace(day.ValidatedPart2) &&
                                   part2 != day.ValidatedPart2;

                invalidCount += part1Invalid ? 1 : 0;
                invalidCount += part2Invalid ? 1 : 0;

                var part1InvalidString = part1Invalid ? " INVALID" : "";
                var part2InvalidString = part2Invalid ? " INVALID" : "";

                Console.WriteLine($"Day {day.DayNumber} Part 1: {part1}{part1InvalidString}");
                Console.WriteLine($"Day {day.DayNumber} Part 2: {part2}{part2InvalidString}");
            }

            Console.WriteLine($"{invalidCount} INVALID Results");
        }
        public void SolveWithTestInput()
        {
            var testInput = @".#.
..#
###".Split("\r\n");
            var cube      = new ConwayCube(testInput);

            for (var n = 0; n < 6; n++)
            {
                cube = cube.Mutate(3);
            }
            var part1 = cube.ActiveCells;

            Assert.AreEqual(112, part1);
        }
        public (string, string) Solve(string[] input)
        {
            var cube = new ConwayCube(input);

            for (var n = 0; n < 6; n++)
            {
                cube = cube.Mutate(3);
            }
            var part1 = cube.ActiveCells;

            cube = new ConwayCube(input);
            for (var n = 0; n < 6; n++)
            {
                cube = cube.Mutate(4);
            }
            var part2 = cube.ActiveCells;

            return(part1.ToString(), part2.ToString());
        }
示例#8
0
            public HashSet <ConwayCube> GetNeighbors(ConwayCube currentCube)
            {
                HashSet <ConwayCube> conwayCubes = new HashSet <ConwayCube>();

                for (int x1 = currentCube.X - 1; x1 <= currentCube.X + 1; x1++)
                {
                    for (int y1 = currentCube.Y - 1; y1 <= currentCube.Y + 1; y1++)
                    {
                        for (int z1 = currentCube.Z - 1; z1 <= currentCube.Z + 1; z1++)
                        {
                            if (x1 != currentCube.X || y1 != currentCube.Y || z1 != currentCube.Z)
                            {
                                conwayCubes.Add(new ConwayCube {
                                    X = x1, Y = y1, Z = z1
                                });
                            }
                        }
                    }
                }

                return(conwayCubes);
            }