Example #1
0
        public void Solve()
        {
            var         length = this.inputs.Length;
            var         store  = new IntBucketSet(this.inputs);
            Span <byte> bits   = stackalloc byte[2020];

            for (int i = 0; i < length; i++)
            {
                bits[this.inputs[i]] = 1;
            }

            for (int i = 0; i < length; i++)
            {
                for (int j = i + 1; j < length; j++)
                {
                    var requiredValue = this.target - (this.inputs[i] + this.inputs[j]);
                    if (requiredValue < 0 || requiredValue >= 2020)
                    {
                        continue;
                    }

                    if (bits[requiredValue] == 0)
                    {
                        continue;
                    }

                    Log.Information("Match: {A} * {B} * {C} = {D}", this.inputs[i], this.inputs[j], requiredValue, this.inputs[i] * this.inputs[j] * requiredValue);
                    return;
                }
            }
        }
Example #2
0
        public void Solve()
        {
            var length     = this.inputs.Length;
            var kAndlPairs = new List <int>(GetTriangleNumber(this.inputs.Length));

            for (int i = 0; i < length; i++)
            {
                for (int j = i + 1; j < length; j++)
                {
                    kAndlPairs.Add(this.inputs[i] + this.inputs[j]);
                }
            }

            var store = new IntBucketSet(kAndlPairs);

            for (int i = 0; i < length; i++)
            {
                for (int j = i + 1; j < length; j++)
                {
                    var requiredValue = this.target - (this.inputs[i] + this.inputs[j]);
                    if (!store.Contains(requiredValue))
                    {
                        continue;
                    }

                    Log.Information("Match: {A} * {B} * {C} = {D}", this.inputs[i], this.inputs[j], requiredValue, this.inputs[i] * this.inputs[j] * requiredValue);
                    return;
                }
            }
        }
        public void Solve()
        {
            Array.Sort(this.inputs);

            var length = this.inputs.Length;
            var store  = new IntBucketSet(this.inputs);

            for (int i = length - 1; i >= 0; i--)
            {
                var iValue = this.inputs[i];
                for (int j = length - 1; j >= i + 1; j--)
                {
                    var jValue        = this.inputs[j];
                    var requiredValue = this.target - (iValue + jValue);
                    if (!store.Contains(requiredValue))
                    {
                        continue;
                    }

                    Log.Information("Match: {A} * {B} * {C} = {D}", iValue, jValue, requiredValue, iValue * jValue * requiredValue);
                    return;
                }
            }
        }