예제 #1
0
            public void ExplodePair(SnailFishElement element)
            {
                if (element.LeftChild == null || element.LeftChild.IsPair)
                {
                    throw new Exception("Left child of exploding pair is also a pair.");
                }
                if (element.RightChild == null || element.RightChild.IsPair)
                {
                    throw new Exception("Right child of exploding pair is also a pair.");
                }

                var leftElement = element.GetRegularElementToLeft();

                if (leftElement != null)
                {
                    leftElement.RegularNumber += element.LeftChild.RegularNumber;
                }

                var rightElement = element.GetRegularElementToRight();

                if (rightElement != null)
                {
                    rightElement.RegularNumber += element.RightChild.RegularNumber;
                }

                RemoveElementFromAllElements(element.LeftChild);
                RemoveElementFromAllElements(element.RightChild);

                element.RightChild    = null;
                element.LeftChild     = null;
                element.RegularNumber = 0;
            }
예제 #2
0
            public SnailFishPairEquation(string line)
            {
                Root = new SnailFishElement(null);
                int startCharIndex = 1;

                DoParse(Root, line, ref startCharIndex);
                AllElaments.Add(Root);
            }
예제 #3
0
            void DoParse(SnailFishElement parent, string line, ref int charIndex)
            {
                var leftChild   = new SnailFishElement(parent);
                var currentChar = line[charIndex++];

                if (currentChar == '[')
                {
                    DoParse(leftChild, line, ref charIndex);
                }
                else if (currentChar >= '0' && currentChar <= '9')
                {
                    leftChild.RegularNumber = currentChar - '0';
                }
                else
                {
                    throw new Exception($"Expected digit or '[' but was '{currentChar}'");
                }


                if (line[charIndex++] != ',')
                {
                    throw new Exception($"Expected ',' but was {line[charIndex - 1]}");
                }

                var rightChild = new SnailFishElement(parent);

                currentChar = line[charIndex++];
                if (currentChar == '[')
                {
                    DoParse(rightChild, line, ref charIndex);
                }
                else if (currentChar >= '0' && currentChar <= '9')
                {
                    rightChild.RegularNumber = currentChar - '0';
                }
                else
                {
                    throw new Exception($"Expected digit or '[' but was '{currentChar}'");
                }

                if (line[charIndex++] != ']')
                {
                    throw new Exception($"Expected ']' but was {line[charIndex - 1]}");
                }

                parent.LeftChild  = leftChild;
                parent.RightChild = rightChild;
                AllElaments.Add(leftChild);
                AllElaments.Add(rightChild);
            }
예제 #4
0
            public void SplitElement(SnailFishElement element)
            {
                var leftChild = new SnailFishElement(element);

                leftChild.RegularNumber = (int)Math.Floor((float)element.RegularNumber / 2);
                AllElaments.Add(leftChild);
                element.LeftChild = leftChild;

                var rightChild = new SnailFishElement(element);

                rightChild.RegularNumber = (int)Math.Ceiling((float)element.RegularNumber / 2);
                AllElaments.Add(rightChild);
                element.RightChild = rightChild;

                element.RegularNumber = -1;
            }
예제 #5
0
            public void Add(SnailFishPairEquation other)
            {
                foreach (var element in other.AllElaments)
                {
                    AllElaments.Add(element);
                }

                foreach (var element in AllElaments)
                {
                    element.Depth++;
                }


                var newRoot = new SnailFishElement(null);

                newRoot.LeftChild  = Root;
                newRoot.RightChild = other.Root;
                Root.Parent        = newRoot;
                other.Root.Parent  = newRoot;
                Root = newRoot;
                AllElaments.Add(newRoot);
            }