Created
December 6, 2019 04:57
-
-
Save decriptor/d9788a31a9272422c522ef33dc558662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Day12 | |
{ | |
class PlantGenerations | |
{ | |
public string InitialState { get; } | |
public Dictionary<string, char> Rules { get; } | |
public int NumberOfGenerations { get; } | |
public Dictionary<int, string> Generations; | |
public PlantGenerations(string initialState, Dictionary<string, char> rules, int numberOfGens) | |
{ | |
InitialState = initialState; | |
Rules = rules; | |
NumberOfGenerations = numberOfGens; | |
Generations = new Dictionary<int, string>(NumberOfGenerations) { | |
{ 0, initialState } | |
}; | |
} | |
public void Run () | |
{ | |
Console.WriteLine("I've run"); | |
int genCount = 0; | |
while (genCount < NumberOfGenerations) | |
{ | |
var originalGen = $"..{Generations[genCount]}.."; | |
var nextGen = new StringBuilder (originalGen); | |
for (int i = 2; i < originalGen.Length - 3; i++) | |
{ | |
var segment = originalGen.Substring(i - 2, 5); | |
if (Rules.ContainsKey(segment)) | |
nextGen[i] = Rules[segment]; | |
else | |
nextGen[i] = '.'; | |
} | |
genCount++; | |
Generations.Add(genCount, nextGen.ToString()); | |
} | |
PrintGenerations(); | |
PrintPotCount(); | |
} | |
public void PrintGenerations () | |
{ | |
foreach (var gen in Generations) | |
{ | |
Console.WriteLine($"{gen.Key:00} -> {gen.Value}"); | |
} | |
} | |
public void PrintPotCount() | |
{ | |
int count = 0; | |
foreach (var genValues in Generations.Values) | |
{ | |
count += genValues.Count(x => x == '#'); | |
} | |
Console.WriteLine($"Pot count: {count}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment