public PlantGeneration EvolveGeneration(PlantGeneration generation) { var pots = $"....{generation.PlantPots}...."; //foreach pot (where there are at least 2 pots before it or after it) //get the pot sequence i-2, i-1, i, i+1, i+2 //lookup the rule //append the answer to the output //also need to track the offset... var builder = new StringBuilder(); for (var i = 2; i < pots.Length - 2; i++) { var pot = Enumerable.Range(-2, 5).Aggregate(new StringBuilder(), (sb, x) => sb.Append(pots[i + x])).ToString(); builder.Append(Rules.TryGetValue(pot, out var plant) ? plant : "."); } var next = builder.ToString(); var result = new PlantGeneration { Offset = generation.Offset, PlantPots = next }; result.Offset -= 2; return(result); }
public PlantGeneration SolveForGenerations(int generations) { var result = new PlantGeneration { PlantPots = InitialState }; return(Enumerable.Range(0, generations).Aggregate(result, (current, generation) => EvolveGeneration(current))); }
public long CountPlants(PlantGeneration generation) { return(Enumerable.Range(0, generation.PlantPots.Length) .Sum(x => generation.PlantPots[x] == '#' ? x + generation.Offset : 0)); }