Initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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<Cell> cells)
|
||||
{
|
||||
return new Pattern(cells.ToList());
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/a/63820524
|
||||
public static IEnumerable<string> SplitByLine(this string str)
|
||||
{
|
||||
return Regex
|
||||
.Split(str, @"((\r)+)?(\n)+((\r)+)?")
|
||||
.Select(i => i.Trim())
|
||||
.Where(i => !string.IsNullOrEmpty(i));
|
||||
}
|
||||
|
||||
public static IEnumerable<Pattern> FindOscillation(this ILife gol, int maxPeriod = 50)
|
||||
{
|
||||
var list = new List<Pattern>();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user