{"id":262302,"date":"2015-07-26T20:03:02","date_gmt":"2015-07-26T16:03:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=262302"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=262302","title":{"rendered":"\u0421 LINQ\u043e\u043c \u043f\u043e \u00ab\u0416\u0438\u0437\u043d\u0438\u00bb"},"content":{"rendered":"<p>     \t\u0417\u043d\u0430\u043c\u0435\u043d\u0438\u0442\u0430\u044f \u0438\u0433\u0440\u0430 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9A%D0%BE%D0%BD%D0%B2%D0%B5%D0%B9,_%D0%94%D0%B6%D0%BE%D0%BD_%D0%A5%D0%BE%D1%80%D1%82%D0%BE%D0%BD\">\u0414\u0436\u043e\u043d\u0430 \u041a\u043e\u043d\u0432\u0435\u044f<\/a> <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%96%D0%B8%D0%B7%D0%BD%D1%8C_%28%D0%B8%D0%B3%D1%80%D0%B0%29\">\u00ab\u0416\u0438\u0437\u043d\u044c\u00bb<\/a>, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0435, \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0443\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c\u0438 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437, \u0447\u0442\u043e \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0441\u043b\u043e\u0432\u0443\u0442\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u00ab\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c\u00bb. <\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0434\u0443, \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u0433\u0440\u044b, \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Language_Integrated_Query\">LINQ<\/a> \u0432 \u0441\u0440\u0435\u0434\u0435 .NET \u2014 \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u0439, \u0431\u0435\u0437 \u0446\u0438\u043a\u043b\u043e\u0432 \u0438 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.<\/p>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u0421\u0440\u0430\u0437\u0443 \u043e\u0433\u043e\u0432\u043e\u0440\u044e\u0441\u044c, \u0447\u0442\u043e <i>\u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u0441\u0442\u0430\u0432\u0438\u043b\u0430\u0441\u044c<\/i>.<\/p>\n<pre><code class=\"cs\">using System; using System.Collections; using System.Collections.Generic; using System.Linq;  namespace LinqLife {     public struct Cell     {         public readonly int X, Y;          public Cell(int x, int y) { X = x; Y = y; }     }      public class Life : IEnumerable&lt;Cell&gt;     {         private List&lt;Cell&gt; _cells = new List&lt;Cell&gt;();          private static readonly int[] Liveables = { 2, 3 };          public bool Next()         {             var died = _cells                 .Where(cell =&gt; !Liveables.Contains(Count(cell)))    \/\/ \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0441\u043c\u0435\u0440\u0442\u0438                 .ToArray();              var born = _cells                 .SelectMany(Ambit)                                  \/\/ \u0412\u0441\u0435 \u043e\u043a\u0440\u0443\u0436\u0430\u044e\u0449\u0438\u0435 \u044f\u0447\u0435\u0439\u043a\u0438                 .Distinct()                                         \/\/ ...\u0431\u0435\u0437 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432                 .Except(_cells)                                     \/\/ ...\u043f\u0443\u0441\u0442\u044b\u0435                 .Where(cell =&gt; Count(cell) == 3)                    \/\/ \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f                 .ToArray();              if (died.Length == 0 && born.Length == 0)                 return false; \/\/ \u041d\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439              _cells = _cells                 .Except(died)                 .Concat(born)                 .ToList();              return _cells.Any(); \/\/ \u041d\u0435 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0443\u043c\u0435\u0440\u043b\u0438?         }          private int Count(Cell cell)         {             return Ambit(cell)                 .Intersect(_cells)                 .Count();         }          private static IEnumerable&lt;Cell&gt; Ambit(Cell cell)         {             return Enumerable.Range(-1, 3)                 .SelectMany(x =&gt; Enumerable.Range(-1, 3)                     .Where(y =&gt; x != 0 || y != 0) \t\t\t\t\t\/\/ \u041a\u0440\u043e\u043c\u0435 \u0446\u0435\u043d\u0442\u0440\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438                     .Select(y =&gt; new Cell(cell.X + x, cell.Y + y)));         }          public override string ToString()         {             if (_cells.Count == 0)                 return string.Empty;              var xmin = _cells.Min(cell =&gt; cell.X);             var xmax = _cells.Max(cell =&gt; cell.X);             var ymin = _cells.Min(cell =&gt; cell.Y);             var ymax = _cells.Max(cell =&gt; cell.Y);              var matrix = Enumerable.Range(xmin, xmax - xmin + 1)                 .Select(x =&gt; Enumerable.Range(ymin, ymax - ymin + 1)                     .Select(y =&gt; _cells.Contains(new Cell(x, y))));              return string.Join(Environment.NewLine,                 matrix.Select(row =&gt;                     string.Join(&quot;&quot;,                         row.Select(b =&gt; b ? &quot;X&quot; : &quot;.&quot;))));         }          public IEnumerator&lt;Cell&gt; GetEnumerator()         {             return _cells.GetEnumerator();         }          IEnumerator IEnumerable.GetEnumerator()         {             return GetEnumerator();         }          public void Add(int x, int y)         {             _cells.Add(new Cell(x, y));         }     }      class Program     {         static void Main(string[] args)         {             var life = new Life { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 1, 2 }, { 1, 3 } };                          var i = 0;             do             {                 Console.WriteLine(&quot;#{0}\\r\\n{1}&quot;, i++, life);                  Console.ReadLine();              } while (life.Next());              Console.WriteLine(life.Any() ? &quot;* Stagnation!&quot; : &quot;* Extinction!&quot;);         }     } } <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043a \u043a\u043e\u0434\u0443:<\/h4>\n<p>  <\/p>\n<ol>\n<li>\u0422\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0433\u0440\u044b \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u0436\u0438\u0432\u044b\u0445 \u043a\u043b\u0435\u0442\u043e\u043a, \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435. \u0412 \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\u0445 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0432 \u0432\u0438\u0434\u0435 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430.<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438: \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435, \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0435, \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435.<\/li>\n<li>\u041d\u0438 \u0435\u0434\u0438\u043d\u043e\u0433\u043e \u044f\u0432\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 (\u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <b>Life<\/b>).<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <b>struct <\/b>\u0434\u043b\u044f <b>Cell<\/b> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435 \u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f \u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u044f\u0447\u0435\u0435\u043a \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e (.NET \u043d\u0430\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <b>ValueType <\/b>\u0432 \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u0445).<\/li>\n<li>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f. \u041f\u0440\u0438\u0447\u0435\u043c \u00ab\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u00bb.<\/li>\n<li>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f: \u0437\u0430\u043c\u0435\u043d\u0430 \u0442\u0438\u043f <b>_cell<\/b> \u0441 <b>List<\/b> \u043d\u0430 <b>HashSet<\/b> \u0438 \u0432\u044b\u0437\u043e\u0432\u044b <b>Except()<\/b>\/<b>Concat()<\/b> \u0432 <b>Next()<\/b> \u043d\u0430 \u0446\u0438\u043a\u043b\u044b \u0441 <b>Remove()<\/b>\/<b>Add()<\/b> \u0434\u0430\u0441\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 (\u043f\u0440\u0430\u0432\u0434\u0430, \u0432 \u0443\u0449\u0435\u0440\u0431 \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430).<\/li>\n<li>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f: <b>AsParallel()<\/b>.<\/li>\n<li>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442 <b>IEnumerable<\/b> \u0438 \u043c\u0435\u0442\u043e\u0434 <b>Add()<\/b> \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0434\u0438 \u0438\u0437\u044f\u0449\u0435\u0441\u0442\u0432\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/li>\n<\/ol>\n<p>  \u041f\u043e\u043b\u0430\u0433\u0430\u044e, \u043c\u0430\u043b\u043e \u043a\u0442\u043e \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e LINQ \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043b\u0435\u0433\u043a\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u0439, \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434. \u041f\u0443\u0441\u0442\u044c \u0441\u0435\u0439 \u043d\u0435\u0445\u0438\u0442\u0440\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0441\u043b\u0443\u0436\u0438\u0442 \u0435\u0449\u0435 \u043e\u0434\u043d\u0438\u043c \u0441\u0432\u0438\u0434\u0435\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e\u043c \u0442\u043e\u043c\u0443.     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/263569\/\"> http:\/\/habrahabr.ru\/post\/263569\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u0417\u043d\u0430\u043c\u0435\u043d\u0438\u0442\u0430\u044f \u0438\u0433\u0440\u0430 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9A%D0%BE%D0%BD%D0%B2%D0%B5%D0%B9,_%D0%94%D0%B6%D0%BE%D0%BD_%D0%A5%D0%BE%D1%80%D1%82%D0%BE%D0%BD\">\u0414\u0436\u043e\u043d\u0430 \u041a\u043e\u043d\u0432\u0435\u044f<\/a> <a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%96%D0%B8%D0%B7%D0%BD%D1%8C_%28%D0%B8%D0%B3%D1%80%D0%B0%29\">\u00ab\u0416\u0438\u0437\u043d\u044c\u00bb<\/a>, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0435, \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0443\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430\u043c\u0438 \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437, \u0447\u0442\u043e \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u0441\u043b\u043e\u0432\u0443\u0442\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u00ab\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c\u00bb. <\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0434\u0443, \u0442\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0438\u0433\u0440\u044b, \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 <a href=\"https:\/\/ru.wikipedia.org\/wiki\/Language_Integrated_Query\">LINQ<\/a> \u0432 \u0441\u0440\u0435\u0434\u0435 .NET \u2014 \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u0439, \u0431\u0435\u0437 \u0446\u0438\u043a\u043b\u043e\u0432 \u0438 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-262302","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262302"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262302\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}