예제 #1
0
 public HouseDecorator(int gridSize)
 {
     _lightsArrays = new ChristmasLight[gridSize][];
     for (int i = 0; i < gridSize; i++)
     {
         _lightsArrays[i] = new ChristmasLight[gridSize];
         //for (int j = 0; j < _lightsArrays.Length; j++)
         //{
         //    _lightsArrays[i][j] = new ChristmasLight();
         //}
     }
 }
예제 #2
0
        public int ChangeBrightness(List <string> instructions)
        {
            var lightsOn = 0;

            foreach (var instruction in instructions)
            {
                var instructionArray = instruction.Split(' ');
                var coordinates      = GetLightCoordinates(instructionArray);


                for (int i = coordinates["x_start"]; i <= coordinates["x_end"]; i++)
                {
                    for (int j = coordinates["y_start"]; j <= coordinates["y_end"]; j++)
                    {
                        if (_lightsArrays[i][j] == null)
                        {
                            _lightsArrays[i][j] = new ChristmasLight();
                        }
                        if (instructionArray.Contains("on"))
                        {
                            _lightsArrays[i][j].Brightness++;
                        }

                        else if (instructionArray.Contains("off"))
                        {
                            _lightsArrays[i][j].Brightness = _lightsArrays[i][j].Brightness == 0
                                ? 0
                                : _lightsArrays[i][j].Brightness - 1;
                        }

                        else if (instructionArray.Contains("toggle"))
                        {
                            _lightsArrays[i][j].Brightness += 2;
                        }
                    }
                }
            }

            foreach (var lightsArray in _lightsArrays)
            {
                lightsOn += lightsArray.Where(l => l != null).Sum(light => light.Brightness);
            }

            return(lightsOn);
        }