using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using GameOfLife.Entities; namespace GameOfLife { public static class Util { public static Pattern ToPattern(this IEnumerable cells) { return new Pattern(cells.ToList()); } //https://stackoverflow.com/a/63820524 public static IEnumerable SplitByLine(this string str) { return Regex .Split(str, @"((\r)+)?(\n)+((\r)+)?") .Select(i => i.Trim()) .Where(i => !string.IsNullOrEmpty(i)); } public static IEnumerable FindOscillation(this ILife gol, int maxPeriod = 50) { var list = new List(); while (gol.Generation < maxPeriod) { var currentPattern = gol.LivingCells.ToPattern().Normalize(); if (list.Contains(currentPattern)) return list; list.Add(currentPattern); gol.IncrementGeneration(); } return list.Take(1); // didn't find any oscillations, return the first pattern } } }