Ejemplo n.º 1
0
        public void Split()
        {
            //To split a regular number, replace it with a pair;
            //the left element of the pair should be the regular number divided by two and rounded down,
            //while the right element of the pair should be the regular number divided by two and rounded up.
            //For example, 10 becomes [5,5], 11 becomes [5,6], 12 becomes [6,6], and so on.
            var half = Math.DivRem(Number, 2, out int remainder);

            Number = half;
            var right          = new SnailfishRegularNumber(half + remainder);
            var currentParent  = Parent;
            var currenPosition = NumberPosition;
            SnailfishNumberPair replacement = new SnailfishNumberPair(this, right);

            replacement.Parent         = currentParent;
            replacement.NumberPosition = currenPosition;
            if (currenPosition == PairPosition.Left)
            {
                currentParent.LeftNumber = replacement;
            }
            else
            {
                currentParent.RightNumber = replacement;
            }
        }
Ejemplo n.º 2
0
        public List <SnailfishNumber> ParseFile(string filePath)
        {
            var numbers = new List <SnailfishNumber>();

            foreach (var line in ReadData(filePath))
            {
                SnailfishNumberPair currentPair = ParseNumber(line);
                numbers.Add(currentPair);
            }
            return(numbers);
        }
Ejemplo n.º 3
0
        public void SingleExplode()
        {
            //[[[[[9,8],1],2],3],4] => [[[[0,9],2],3],4]
            var pair  = new SnailfishNumberPair(new SnailfishRegularNumber(9), new SnailfishRegularNumber(8));
            var pair2 = new SnailfishNumberPair(pair, new SnailfishRegularNumber(1));
            var pair3 = new SnailfishNumberPair(pair2, new SnailfishRegularNumber(2));
            var pair4 = new SnailfishNumberPair(pair3, new SnailfishRegularNumber(3));
            var pair5 = new SnailfishNumberPair(pair4, new SnailfishRegularNumber(4));

            pair5.Reduce();
            Assert.AreEqual(true, pair2.LeftNumber is SnailfishRegularNumber);
            Assert.AreEqual(true, pair2.RightNumber is SnailfishRegularNumber);
            Assert.AreEqual(0, ((SnailfishRegularNumber)pair2.LeftNumber).Number);
            Assert.AreEqual(9, ((SnailfishRegularNumber)pair2.RightNumber).Number);
        }
Ejemplo n.º 4
0
        public void SingleMagnitude()
        {
            //[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]
            //    \1/   \2/     \3/   \4/       \5/   \6/    \7/
            //     ***8***       ***9***         ***10**      *
            //        ******11******                *****12****
            //              ************13***************
            var pair1 = new SnailfishNumberPair(new SnailfishRegularNumber(8), new SnailfishRegularNumber(7));
            var pair2 = new SnailfishNumberPair(new SnailfishRegularNumber(7), new SnailfishRegularNumber(7));
            var pair3 = new SnailfishNumberPair(new SnailfishRegularNumber(8), new SnailfishRegularNumber(6));
            var pair4 = new SnailfishNumberPair(new SnailfishRegularNumber(7), new SnailfishRegularNumber(7));
            var pair5 = new SnailfishNumberPair(new SnailfishRegularNumber(0), new SnailfishRegularNumber(7));
            var pair6 = new SnailfishNumberPair(new SnailfishRegularNumber(6), new SnailfishRegularNumber(6));
            var pair7 = new SnailfishNumberPair(new SnailfishRegularNumber(8), new SnailfishRegularNumber(7));

            var pair8  = new SnailfishNumberPair(pair1, pair2);
            var pair9  = new SnailfishNumberPair(pair3, pair4);
            var pair10 = new SnailfishNumberPair(pair5, pair6);
            var pair11 = new SnailfishNumberPair(pair8, pair9);
            var pair12 = new SnailfishNumberPair(pair10, pair7);
            var pair13 = new SnailfishNumberPair(pair11, pair12);

            Assert.AreEqual(3488, pair13.Magnitude);
        }
Ejemplo n.º 5
0
        internal static SnailfishNumberPair ParseNumber(string numberString)
        {
            SnailfishNumberPair         currentPair = null;
            Stack <SnailfishNumberPair> pairs       = new Stack <SnailfishNumberPair>();

            foreach (var letter in numberString)
            {
                switch (letter)
                {
                case '[':
                    //add pair
                    if (currentPair != null)
                    {
                        pairs.Push(currentPair);
                    }
                    currentPair = new SnailfishNumberPair();
                    var parent = pairs.Count > 0 ? pairs.Peek() : null;
                    if (parent != null)
                    {
                        currentPair.Parent = parent;
                        if (parent?.LeftNumber == null)
                        {
                            parent.SetLeft(currentPair);
                        }
                        else
                        {
                            parent.SetRight(currentPair);
                        }
                    }
                    break;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    var number = new SnailfishRegularNumber(Convert.ToInt32(letter.ToString()));
                    if (currentPair?.LeftNumber == null)
                    {
                        currentPair.SetLeft(number);
                    }
                    else
                    {
                        currentPair.SetRight(number);
                    }
                    break;

                case ']':
                    currentPair = pairs.Count > 0 ? pairs.Pop() : currentPair;
                    break;

                case ',':
                //do nothing, logic is in set number
                default:
                    break;
                }
            }

            return(currentPair);
        }