Пример #1
0
        public int Problem1(string input)
        {
            var map = Mapper.ConvertToMap(input, Convert);

            for (var loop = 0; loop < 10; loop++)
            {
                var newMap = new TileState[map.GetLength(0), map.GetLength(1)];

                for (var i = 0; i < map.GetLength(0); i++)
                {
                    for (var j = 0; j < map.GetLength(1); j++)
                    {
                        var surrounding = GetNeighbours(map, i, j);
                        var current     = map[i, j];
                        newMap[i, j] = current;

                        if (current == TileState.Open && surrounding.Count(it => it == TileState.Trees) >= 3)
                        {
                            newMap[i, j] = TileState.Trees;
                        }

                        if (current == TileState.Trees && surrounding.Count(it => it == TileState.Lumber) >= 3)
                        {
                            newMap[i, j] = TileState.Lumber;
                        }

                        if (current == TileState.Lumber && (!surrounding.Any(it => it == TileState.Lumber) || !surrounding.Any(it => it == TileState.Trees)))
                        {
                            newMap[i, j] = TileState.Open;
                        }
                    }
                }

                map = newMap;
                Console.WriteLine("After" + (loop + 1) + " minutes");
                Dumper.DumpMap(map, ConvertBack);
            }

            var totalTrees  = 0;
            var totalLumber = 0;

            for (var i = 0; i < map.GetLength(0); i++)
            {
                for (var j = 0; j < map.GetLength(1); j++)
                {
                    if (map[i, j] == TileState.Trees)
                    {
                        totalTrees++;
                    }
                    else if (map[i, j] == TileState.Lumber)
                    {
                        totalLumber++;
                    }
                }
            }

            return(totalTrees * totalLumber);
        }