Example #1
0
        public void TransferTo(Bucket bucket)
        {
            int transfer = 0;

            if(this.Level <= bucket.Remaining)
            {
                transfer = this.Level;
            }

            else if(this.Level > bucket.Remaining)
            {
                transfer = bucket.Remaining;
            }

            bucket.Level += transfer;
            this.Level -= transfer;
        }
Example #2
0
        private Solution SolveDown(int bucket1Capacity, int bucket2Capacity, int desiredLevel)
        {
            if (bucket1Capacity == bucket2Capacity)
            {
                throw new Exception();
            }

            int smallBucketCapacity = bucket1Capacity < bucket2Capacity ? bucket1Capacity : bucket2Capacity;
            int largeBucketCapacity = bucket1Capacity > bucket2Capacity ? bucket1Capacity : bucket2Capacity;

            Bucket smallBucket = new Bucket(smallBucketCapacity);
            Bucket largeBucket = new Bucket(largeBucketCapacity);
            List<Step> steps = new List<Step>();

            while ((smallBucket.Level != desiredLevel) && (largeBucket.Level != desiredLevel) && (smallBucket.Level + largeBucket.Level != desiredLevel))
            {
                Step step;

                if (smallBucket.IsEmpty)
                {
                    smallBucket.Fill();
                    step = new Step(smallBucket.Level, largeBucket.Level, Action.FILL);
                }
                else if (!largeBucket.IsFull)
                {
                    smallBucket.TransferTo(largeBucket);
                    step = new Step(smallBucket.Level, largeBucket.Level, Action.TRANSFER);
                }
                else
                {
                    largeBucket.Dump();
                    step = new Step(smallBucket.Level, largeBucket.Level, Action.DUMP);
                }

                if(steps.Exists(x => x.SmallBucketLevel == step.SmallBucketLevel && x.LargeBucketLevel == step.LargeBucketLevel))
                {
                    return new Solution(false, steps);
                }

                steps.Add(step);
            }

            return new Solution(true, steps);
        }