コード例 #1
0
        public Day21Pattern[][] BreakIntoSquares()
        {
            var eachSquareSize = (Size % 2 == 0 ? 2 : 3);

            Day21Pattern[][] squares = new Day21Pattern[Size / eachSquareSize][];
            for (int i = 0; i < Size / eachSquareSize; i++)
            {
                squares[i] = new Day21Pattern[Size / eachSquareSize];
                for (int j = 0; j < Size / eachSquareSize; j++)
                {
                    bool[][] thisSquarePattern = new bool[eachSquareSize][];

                    for (int k = 0; k < eachSquareSize; k++)
                    {
                        thisSquarePattern[k] = new bool[eachSquareSize];
                        for (int l = 0; l < eachSquareSize; l++)
                        {
                            thisSquarePattern[k][l] = Pattern[i * eachSquareSize + k][j * eachSquareSize + l];
                        }

                        squares[i][j] = new Day21Pattern(thisSquarePattern);
                    }
                }
            }

            return(squares);
        }
コード例 #2
0
        public Day21Pattern EnhancePattern(Day21Pattern pattern)
        {
            var encodedPatterns = new[]
            {
                pattern.ToEncodedPattern(),
                pattern.FlipHorizontal().ToEncodedPattern(),
                pattern.FlipHorizontal().RotateLeft().ToEncodedPattern(),
                pattern.FlipVertical().RotateRight().ToEncodedPattern(),
                pattern.FlipVertical().RotateLeft().ToEncodedPattern(),
                pattern.FlipHorizontal().RotateRight().ToEncodedPattern(),
                pattern.FlipVertical().ToEncodedPattern(),
                pattern.RotateLeft().ToEncodedPattern(),
                pattern.RotateLeft().RotateLeft().ToEncodedPattern(),
                pattern.RotateRight().ToEncodedPattern()
            };

            foreach (var encodedPattern in encodedPatterns)
            {
                if (rules.ContainsKey(encodedPattern))
                {
                    return(new Day21Pattern(rules[encodedPattern]));
                }
            }


            throw new InvalidOperationException("Oops, found no matching rule! Something went wrong...");
        }
コード例 #3
0
        public int Solve()
        {
            var pattern  = new Day21Pattern();
            var ruleBook = new Day21RuleBook(Input);


            for (int i = 0; i < 18; i++)

            {
                Console.WriteLine(pattern);
                Console.WriteLine("=======");

                var nextPatterns     = pattern.BreakIntoSquares();
                var enhancedPatterns =
                    nextPatterns.Select(patternRow => patternRow.Select(p => ruleBook.EnhancePattern(p)).ToArray()).ToArray();
                var combinedPattern = new Day21Pattern(enhancedPatterns);
                pattern = combinedPattern;
            }
            Console.WriteLine(pattern);
            return(pattern.CountOnPixels()); //240 too high, bother. 210 still too high; I must have misunderstood something as order of rotation / flipping seems to matter. 206 also wrong
        }