Example #1
0
        private bool IsTargetReached(Jug first, Jug second)
        {
            var smallest = first; //first < second ? first : second;
            var biggest  = second;

            return(smallest.IsFilledOn(_targetFillAmount) && biggest.IsFilledOn(0));
        }
Example #2
0
        public int FillFrom(Jug other)
        {
            var fillOn = Capacity - CurrentAmount;

            if (other.CurrentAmount <= fillOn)
            {
                SetCurrentAmount(CurrentAmount + other.CurrentAmount);
                return(0);
            }

            SetCurrentAmount(CurrentAmount + fillOn);
            return(other.CurrentAmount - fillOn);
        }
Example #3
0
        public (bool success, IEnumerable <JugStep> steps) RunJugProcess()
        {
            var steps    = new List <JugStep>();
            var smallJug = new Jug(Math.Min(_firstCapacity, _secondCapacity));
            var bigJug   = new Jug(Math.Max(_firstCapacity, _secondCapacity));
            var success  = true;

            while (!IsTargetReached(smallJug, bigJug))
            {
                if (!bigJug.ClearIfFull())
                {
                    if (!smallJug.IsEmpty)
                    {
                        smallJug.SetCurrentAmount(bigJug.FillFrom(smallJug));
                    }
                    else
                    {
                        smallJug.FillWhole();
                    }
                }

                var step = new JugStep(smallJug.CurrentAmount, bigJug.CurrentAmount);
                steps.Add(step);
                _logger.LogInformation(step.ToString());
                if (steps.Count < _config.MaxIterations)
                {
                    continue;
                }

                if (IsTargetReached(smallJug, bigJug))
                {
                    continue;
                }
                success = false;
                break;
            }

            return(success, steps);
        }
Example #4
0
 protected bool Equals(Jug other)
 {
     return(Capacity == other.Capacity);
 }