Пример #1
0
        int Iterate(string input, int iterations)
        {
            var mtx     = Mtx.FromString(".#./..#/###");
            var ruleset = new RuleSet(input);

            for (var i = 0; i < iterations; i++)
            {
                mtx = ruleset.Apply(mtx);
            }
            return(mtx.Count());
        }
Пример #2
0
        public static Mtx FromString(string st)
        {
            st = st.Replace("/", "");
            var size = (int)Math.Sqrt(st.Length);
            var res  = new Mtx(size);

            for (int i = 0; i < st.Length; i++)
            {
                res[i / size, i % size] = st[i] == '#';
            }
            return(res);
        }
Пример #3
0
        IEnumerable <Mtx> Variations(Mtx mtx)
        {
            for (int j = 0; j < 2; j++)
            {
                for (int i = 0; i < 4; i++)
                {
                    yield return(mtx);

                    mtx = mtx.Rotate();
                }
                mtx = mtx.Flip();
            }
        }
Пример #4
0
        public Mtx Rotate()
        {
            var res = new Mtx(this.Size);

            for (int i = 0; i < Size; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    res[i, j] = this[j, Size - i - 1];
                }
            }
            return(res);
        }
Пример #5
0
        public Mtx Flip()
        {
            var res = new Mtx(this.Size);

            for (int irow = 0; irow < Size; irow++)
            {
                for (int icol = 0; icol < Size; icol++)
                {
                    res[irow, Size - icol - 1] = this[irow, icol];
                }
            }
            return(res);
        }
Пример #6
0
        public RuleSet(string input)
        {
            rules2 = new Dictionary <int, Mtx>();
            rules3 = new Dictionary <int, Mtx>();

            foreach (var line in input.Split('\n'))
            {
                var parts = Regex.Split(line, " => ");
                var left  = parts[0];
                var right = parts[1];
                var rules =
                    left.Length == 5 ? rules2 :
                    left.Length == 11 ? rules3 :
                    throw new Exception();
                foreach (var mtx in Variations(Mtx.FromString(left)))
                {
                    rules[mtx.CodeNumber] = Mtx.FromString(right);
                }
            }
        }
Пример #7
0
        public static Mtx Join(Mtx[] rgmtx)
        {
            var mtxPerRow = (int)Math.Sqrt(rgmtx.Length);
            var res       = new Mtx(mtxPerRow * rgmtx[0].Size);

            for (int imtx = 0; imtx < rgmtx.Length; imtx++)
            {
                var mtx = rgmtx[imtx];
                for (int irow = 0; irow < mtx.Size; irow++)
                {
                    for (int icol = 0; icol < mtx.Size; icol++)
                    {
                        var irowRes = (imtx / mtxPerRow) * mtx.Size + irow;
                        var icolRes = (imtx % mtxPerRow) * mtx.Size + icol;
                        res[irowRes, icolRes] = mtx[irow, icol];
                    }
                }
            }

            return(res);
        }
Пример #8
0
        public IEnumerable <Mtx> Split()
        {
            var blockSize =
                Size % 2 == 0 ? 2 :
                Size % 3 == 0 ? 3 :
                throw new Exception();

            for (int irow = 0; irow < Size; irow += blockSize)
            {
                for (int icol = 0; icol < Size; icol += blockSize)
                {
                    var mtx = new Mtx(blockSize);
                    for (int drow = 0; drow < blockSize; drow++)
                    {
                        for (int dcol = 0; dcol < blockSize; dcol++)
                        {
                            mtx[drow, dcol] = this[irow + drow, icol + dcol];
                        }
                    }
                    yield return(mtx);
                }
            }
        }