Пример #1
0
        public static string[] WhoEatsWho(string zoo)
        {
            List <Animal> animals = new List <Animal>()
            {
                new Animal("antelope", new List <string> {
                    "grass"
                }),
                new Animal("bug", new List <string> {
                    "leaves"
                }),
                new Animal("bear", new List <string> {
                    "leaves", "big-fish", "bug", "chicken", "cow", "sheep"
                }),
                new Animal("chicken", new List <string> {
                    "bug"
                }),
                new Animal("cow", new List <string> {
                    "grass"
                }),
                new Animal("fox", new List <string> {
                    "chicken", "sheep"
                }),
                new Animal("giraffe", new List <string> {
                    "leaves"
                }),
                new Animal("lion", new List <string> {
                    "antelope", "cow"
                }),
                new Animal("panda", new List <string> {
                    "leaves"
                }),
                new Animal("sheep", new List <string> {
                    "grass"
                }),
                new Animal("big-fish", new List <string> {
                    "little-fish"
                })
            };

            ZooLogger.Init(zoo);

            List <string> thingsInZoo = zoo.Split(',').ToList();

            int index = 0;

            while (index < thingsInZoo.Count)
            {
                Animal animal = animals.Find(anim => anim.HasName(thingsInZoo.ElementAtOrDefault(index)));

                index = animal?.EatNearby(ref thingsInZoo, index) ?? index + 1;
            }

            ZooLogger.Add(string.Join(",", thingsInZoo));

            return(ZooLogger.GetAndFlush());
        }
Пример #2
0
        public int EatNearby(ref List <string> thingsInZoo, int animalIndex)
        {
            string leftThing  = thingsInZoo.ElementAtOrDefault(animalIndex - 1);
            string rightThing = thingsInZoo.ElementAtOrDefault(animalIndex + 1);

            if (this.IsAbleToEat(leftThing))
            {
                ZooLogger.Eating(this.Name, leftThing);
                thingsInZoo.RemoveAt(animalIndex - 1);
                animalIndex -= 2;
            }
            else
            {
                while (this.IsAbleToEat(rightThing))
                {
                    ZooLogger.Eating(this.Name, rightThing);
                    thingsInZoo.RemoveAt(animalIndex + 1);
                    rightThing = thingsInZoo.ElementAtOrDefault(animalIndex + 1);
                }
                animalIndex++;
            }

            return(animalIndex);
        }