Example #1
0
            public long GetValue()
            {
                switch (Type)
                {
                case PacketType.Sum:
                    return(SubPackets.Sum(i => i.GetValue()));

                case PacketType.Product:
                    return(SubPackets.Select(i => i.GetValue()).Aggregate((total, next) => total * next));

                case PacketType.Minimum:
                    return(SubPackets.Min(i => i.GetValue()));

                case PacketType.Maximum:
                    return(SubPackets.Max(i => i.GetValue()));

                case PacketType.Literal:
                    return(LiteralValue.Value);

                case PacketType.GreaterThan:
                    if (SubPackets[0].GetValue() > SubPackets[1].GetValue())
                    {
                        return(1);
                    }
                    return(0);

                case PacketType.SmallerThan:
                    if (SubPackets[0].GetValue() < SubPackets[1].GetValue())
                    {
                        return(1);
                    }
                    return(0);

                case PacketType.EqualTo:
                    if (SubPackets[0].GetValue() == SubPackets[1].GetValue())
                    {
                        return(1);
                    }
                    return(0);
                }

                throw new Exception("Unsupported type");
            }
Example #2
0
 public int SumVersion()
 {
     return(SubPackets.Sum(i => i.SumVersion()) + Version);
 }
Example #3
0
            public Packet(String input)
            {
                _input = input;

                Version = Convert.ToInt32(_input.Substring(0, 3), 2);
                TypeID  = Convert.ToInt32(_input.Substring(3, 3), 2);
                if (TypeID == 4)
                {
                    var cursor       = 6;
                    var nextGroup    = _input.Substring(cursor, 5);
                    var decimalValue = "";
                    while (nextGroup != null)
                    {
                        decimalValue += nextGroup.Substring(1);
                        cursor       += 5;
                        if (nextGroup.Substring(0, 1) == "1")
                        {
                            nextGroup = _input.Substring(cursor, 5);
                        }
                        else
                        {
                            nextGroup = null;
                        }
                    }

                    Value  = Convert.ToInt64(decimalValue, 2);
                    Length = cursor;
                    return;
                }
                else
                {
                    LengthType = Convert.ToInt32(_input.Substring(6, 1));

                    if (LengthType == 0)
                    {
                        var subPackagesLenght = Convert.ToInt32(_input.Substring(7, 15), 2);
                        var cursor            = 22;
                        var init_index        = cursor;
                        while (cursor < init_index + subPackagesLenght)
                        {
                            var packet = new Packet(_input.Substring(cursor, _input.Length - cursor));
                            cursor += packet.Length;
                            SubPackets.Add(packet);
                        }
                        Length = cursor;
                    }
                    else
                    {
                        var numberOfSubPackets = Convert.ToInt32(_input.Substring(7, 11), 2);
                        var cursor             = 18;
                        for (int i = 0; i < numberOfSubPackets; i++)
                        {
                            var packet = new Packet(_input.Substring(cursor, _input.Length - cursor));
                            cursor += packet.Length;
                            SubPackets.Add(packet);
                        }

                        Length = cursor;
                    }
                }

                if (TypeID == 0)
                {
                    Value = SubPackets.Select(x => x.Value).Sum();
                }
                else if (TypeID == 1)
                {
                    Value = SubPackets.Select(x => x.Value).Aggregate((x, y) => x * y);
                }
                else if (TypeID == 2)
                {
                    Value = SubPackets.Select(x => x.Value).Min();
                }
                else if (TypeID == 3)
                {
                    Value = SubPackets.Select(x => x.Value).Max();
                }
                else if (TypeID == 5)
                {
                    Value = (SubPackets[0].Value > SubPackets[1].Value) ? 1 : 0;
                }
                else if (TypeID == 6)
                {
                    Value = (SubPackets[0].Value < SubPackets[1].Value) ? 1 : 0;
                }
                else if (TypeID == 7)
                {
                    Value = (SubPackets[0].Value == SubPackets[1].Value) ? 1 : 0;
                }
            }