Пример #1
0
        private bool SplitNumber(SnailNumber number)
        {
            if (number.Value == -1)
            {
                return(SplitNumber(number.Left) || SplitNumber(number.Right));
            }

            if (number.Value >= 10)
            {
                number.Left = new SnailNumber()
                {
                    Parent = number,
                    Value  = (int)Math.Floor((double)number.Value / 2)
                };

                number.Right = new SnailNumber()
                {
                    Parent = number,
                    Value  = (int)Math.Ceiling((double)number.Value / 2)
                };

                number.Value = -1;

                return(true);
            }

            return(false);
        }
        public void SnailNumber_Magnitude_OK(string lna, long expected)
        {
            var  sna    = SnailNumber.Builder(lna);
            long actual = sna.Magnitude();

            Assert.Equal(expected, actual);
        }
        public void Day18_Puzzle2_OK()
        {
            var lines = Utils.ReadLinesFromFile("Day18.txt");

            Assert.Equal(100, lines.Length);
            long maxMag = 0;

            for (int i = 0; i < lines.Length; i++)
            {
                for (int j = 0; j < lines.Length; j++)
                {
                    var sn1  = SnailNumber.Builder(lines[i]);
                    var sn2  = SnailNumber.Builder(lines[j]);
                    var sum1 = sn1.Add(sn2);
                    sum1.Reduce();
                    long mag = sum1.Magnitude();
                    if (maxMag < mag)
                    {
                        maxMag = mag;
                    }

                    var snr1  = SnailNumber.Builder(lines[j]);
                    var snr2  = SnailNumber.Builder(lines[i]);
                    var sumr1 = snr1.Add(snr2);
                    sumr1.Reduce();
                    mag = sumr1.Magnitude();
                    if (maxMag < mag)
                    {
                        maxMag = mag;
                    }
                }
            }
            Assert.Equal(4536, maxMag);
        }
        public void SnailNumber_Parse_OK(string expected)
        {
            SnailNumber sn = SnailNumber.Builder(expected);

            string actual = sn.ToString();

            Assert.Equal(expected, actual);
        }
        public void SnailNumber_Add_OK(string lna, string lnb, string expected)
        {
            var sna    = SnailNumber.Builder(lna);
            var snb    = SnailNumber.Builder(lnb);
            var actual = sna.Add(snb);

            Assert.Equal(expected, actual.ToString());
        }
Пример #6
0
        private int GetNumberMagnitude(SnailNumber number)
        {
            if (number.Value != -1)
            {
                return(number.Value);
            }

            return(3 * GetNumberMagnitude(number.Left) + 2 * GetNumberMagnitude(number.Right));
        }
Пример #7
0
    void ParseData(string[] data)
    {
        homeworkInput = new SnailNumber[data.Length];

        for (int i = 0; i < data.Length; i++)
        {
            homeworkInput[i] = new SnailNumber(data[i]);
        }
    }
        public void SnailNumber_Split_OK()
        {
            string lna      = "[[[[0,7],4],[15,[0,13]]],[1,1]]";
            string expected = "[[[[0,7],4],[[7,8],[0,13]]],[1,1]]";

            var sna = SnailNumber.Builder(lna);
            var sn  = sna.Split();

            Assert.Equal(expected, sna.ToString());
        }
Пример #9
0
        private int CalculateMagnitudOfSum()
        {
            SnailNumber sum = CopyNumber(_numbers[0]);

            for (int i = 1; i < _numbers.Count; i++)
            {
                sum = AddTwoNumbers(sum, CopyNumber(_numbers[i]));
            }

            return(GetNumberMagnitude(sum));
        }
Пример #10
0
        private bool ReduceNumber(SnailNumber number)
        {
            bool somethingExploded = ExplodeNumber(number, 0);

            if (!somethingExploded)
            {
                return(SplitNumber(number));
            }

            return(somethingExploded);
        }
        public void SnailNumber_AddList_ok(string testfile, string expected)
        {
            var         lines = Utils.ReadLinesFromFile(testfile);
            SnailNumber snacc = SnailNumber.Builder(lines[0]);

            for (int i = 1; i < lines.Length; i++)
            {
                SnailNumber sn = SnailNumber.Builder(lines[i]);
                snacc = snacc.Add(sn);
                snacc.Reduce();
            }
            Assert.Equal(expected, snacc.ToString());
        }
        public void SnailNumber_ReduceExplode_OK(string lna, string exploding, string expected)
        {
            var sna = SnailNumber.Builder(lna);

            SnailNumber explode = sna.FindExplode(0);

            Assert.Equal(exploding, explode.ToString());

            var sn = sna.Explode();

            Assert.Equal(exploding, sn.ToString());
            Assert.Equal(expected, sna.ToString());
        }
Пример #13
0
 private void PrintNumber(SnailNumber number)
 {
     if (number.Value != -1)
     {
         Console.Write(number.Value);
     }
     else
     {
         Console.Write("[");
         PrintNumber(number.Left);
         Console.Write(",");
         PrintNumber(number.Right);
         Console.Write("]");
     }
 }
        public void SnailNumber_AddReduce_OK()
        {
            string lna      = "[[[[4,3],4],4],[7,[[8,4],9]]]";
            string lnb      = "[1,1]";
            string expected = "[[[[0,7],4],[[7,8],[6,0]]],[8,1]]";

            var sna = SnailNumber.Builder(lna);
            var snb = SnailNumber.Builder(lnb);

            var sn = sna.Add(snb);

            sn.Reduce();

            Assert.Equal(expected, sn.ToString());
        }
        public void SnailNumber_ListMagnitude_OK()
        {
            var         lines = Utils.ReadLinesFromFile("Day18Test5.txt");
            SnailNumber snacc = SnailNumber.Builder(lines[0]);

            for (int i = 1; i < lines.Length; i++)
            {
                SnailNumber sn = SnailNumber.Builder(lines[i]);
                snacc = snacc.Add(sn);
                snacc.Reduce();
            }
            long actual = snacc.Magnitude();

            Assert.Equal(4140, actual);
        }
Пример #16
0
        private SnailNumber ParseNumber(string line)
        {
            SnailNumber root = new SnailNumber()
            {
                Parent = null,
                Value  = -1
            };

            SnailNumber number = root;

            foreach (char c in line)
            {
                if (c == '[')
                {
                    number.Left = new SnailNumber()
                    {
                        Parent = number,
                        Value  = -1
                    };

                    number = number.Left;
                }

                if (c >= '0' && c <= '9')
                {
                    number.Value = c - '0';
                }

                if (c == ',')
                {
                    number       = number.Parent;
                    number.Right = new SnailNumber()
                    {
                        Parent = number,
                        Value  = -1
                    };

                    number = number.Right;
                }

                if (c == ']')
                {
                    number = number.Parent;
                }
            }

            return(root);
        }
        public void SnailNumber_NothingToExplode_OK()
        {
            string lna      = "[[1,2],[[3,4],5]]";
            string expected = "[[1,2],[[3,4],5]]";

            var sna = SnailNumber.Builder(lna);

            SnailNumber explode = sna.FindExplode(0);

            Assert.Null(explode);

            var sn = sna.Explode();

            Assert.Null(sn);
            Assert.Equal(expected, sna.ToString());
        }
        public void Day18_Puzzle1_OK()
        {
            var lines = Utils.ReadLinesFromFile("Day18.txt");

            Assert.Equal(100, lines.Length);
            SnailNumber snacc = SnailNumber.Builder(lines[0]);

            for (int i = 1; i < lines.Length; i++)
            {
                SnailNumber sn = SnailNumber.Builder(lines[i]);
                snacc = snacc.Add(sn);
                snacc.Reduce();
            }
            long actual = snacc.Magnitude();

            Assert.Equal(4088, actual);
        }
Пример #19
0
 void ReduceNumber(ref SnailNumber number)
 {
     if (!number.AnyNeedsReduce)
     {
         return;
     }
     if (number.AnyNeedsExplode)
     {
         var target = number.FirstNeedsExplode;
         target.Explode();
     }
     else
     {
         var target = number.FirstNeedsReducing;
         target.Split();
     }
 }
Пример #20
0
        private SnailNumber CopyNumber(SnailNumber orig)
        {
            SnailNumber copy = new SnailNumber()
            {
                Value = orig.Value
            };

            if (orig.Value == -1)
            {
                copy.Left  = CopyNumber(orig.Left);
                copy.Right = CopyNumber(orig.Right);

                copy.Left.Parent  = copy;
                copy.Right.Parent = copy;
            }

            return(copy);
        }
Пример #21
0
            public SnailNumber(string rawData, int nestingLevel = 0, SnailNumber parent = null)
            {
                RawData    = rawData;
                NestingLvl = nestingLevel;
                Parent     = parent;

                if (int.TryParse(rawData, out var actualNumber))
                {
                    RegularNumberValue = actualNumber;
                    return;
                }

                var bracketNestingLevel = 0;
                int commaIndex          = 0;

                for (int i = 1; i < rawData.Length - 1; i++)
                {
                    var c = rawData[i];
                    switch (c)
                    {
                    case '[':
                        bracketNestingLevel++;
                        break;

                    case ']':
                        bracketNestingLevel--;
                        break;

                    case ',':
                        if (bracketNestingLevel == 0)
                        {
                            commaIndex = i;
                        }

                        break;
                    }
                }

                var rawItem1 = rawData.Substring(1, commaIndex - 1);
                var rawItem2 = rawData.Substring(commaIndex + 1, rawData.Length - rawItem1.Length - 3);

                Item1 = new SnailNumber(rawItem1, nestingLevel + 1, this);
                Item2 = new SnailNumber(rawItem2, nestingLevel + 1, this);
            }
Пример #22
0
        private bool ExplodeNumber(SnailNumber number, int depth)
        {
            if (!IsLeafPair(number))
            {
                return((number.Left.Value == -1 && ExplodeNumber(number.Left, depth + 1)) || (number.Right.Value == -1 && ExplodeNumber(number.Right, depth + 1)));
            }

            if (depth == 4)
            {
                AddToFirstNumberOnTheLeft(number, number.Left.Value);
                AddToFirstNumberOnTheRight(number, number.Right.Value);

                number.Left  = null;
                number.Right = null;
                number.Value = 0;

                return(true);
            }

            return(false);
        }
Пример #23
0
        private void AddToFirstNumberOnTheLeft(SnailNumber number, int value)
        {
            var nextLeft = number;

            while (nextLeft.Parent != null && nextLeft.Parent.Left == nextLeft)
            {
                nextLeft = nextLeft.Parent;
            }

            if (nextLeft.Parent != null)
            {
                nextLeft = nextLeft.Parent.Left;

                while (nextLeft.Value == -1)
                {
                    nextLeft = nextLeft.Right;
                }

                nextLeft.Value += value;
            }
        }
Пример #24
0
        private SnailNumber AddTwoNumbers(SnailNumber n1, SnailNumber n2)
        {
            SnailNumber n = new SnailNumber()
            {
                Left   = n1,
                Right  = n2,
                Parent = null,
                Value  = -1
            };

            n1.Parent = n;
            n2.Parent = n;

            bool reduced = false;

            do
            {
                reduced = ReduceNumber(n);
            } while (reduced);

            return(n);
        }
Пример #25
0
        private int GetLargestMagnitudOfAnyTwoNumbers()
        {
            int maxMagnitud = 0;

            for (int i = 0; i < _numbers.Count; i++)
            {
                for (int j = 0; j < _numbers.Count; j++)
                {
                    if (i != j)
                    {
                        SnailNumber sum      = AddTwoNumbers(CopyNumber(_numbers[i]), CopyNumber(_numbers[j]));
                        int         magnitud = GetNumberMagnitude(sum);

                        if (maxMagnitud < magnitud)
                        {
                            maxMagnitud = magnitud;
                        }
                    }
                }
            }

            return(maxMagnitud);
        }
Пример #26
0
 private bool IsLeafPair(SnailNumber number)
 {
     return(number.Left.Value != -1 && number.Right.Value != -1);
 }
Пример #27
0
 public SnailNumber Add(SnailNumber other)
 {
     return(new SnailNumber($"[{this.ToString()},{other.ToString()}]").Reduce());
 }