private static (char startState, int nSteps, RuleDictionary rules) Parse(string input) { var lines = input.Split('\n').Select(x => x.TrimEnd()).ToList(); var rules = new RuleDictionary(); void ParseState(int start) { var dict = new Dictionary <int, (int newValue, int move, char newState)>(); foreach (int i in new[] { 1, 5 }) { var value = lines[start + i][lines[start + i].Length - 2] - '0'; var newValue = lines[start + i + 1][lines[start + i + 1].Length - 2] - '0'; var move = lines[start + i + 2].EndsWith("left.") ? -1 : 1; var newState = lines[start + i + 3][lines[start + i + 3].Length - 2]; dict[value] = (newValue, move, newState); } rules[lines[start][9]] = dict; } for (int i = 3; i < lines.Count; i++) { if (lines[i].StartsWith("In state ")) { ParseState(i); } } char startState = lines[0][lines[0].IndexOf('.') - 1]; int iAfter = lines[1].IndexOf("after", StringComparison.Ordinal); int iSteps = lines[1].IndexOf(" steps", StringComparison.Ordinal); int nSteps = int.Parse(lines[1].Substring(iAfter + 6, iSteps - iAfter - 6)); return(startState, nSteps, rules); }
public Piece Run(PieceGrid currentGen, Point point, Piece piece) { PointHelpers.NeighborhoodOrder neighborhoodOrder; if (Neighborhood == CANeighborhood.Moore) { neighborhoodOrder = PointHelpers.NeighborhoodOrder.MooreRuleTable; } else { neighborhoodOrder = PointHelpers.NeighborhoodOrder.VonNeumannRuleTable; } IEnumerable <Point> neighborhoodPoints = PointHelpers.GetAdjacentPointsToroid(point, currentGen, neighborhoodOrder); List <int> neighborhood = new List <int>(); neighborhood.Add(piece.StateValue); foreach (Point p in neighborhoodPoints) { neighborhood.Add(currentGen.PointPieces[p].StateValue); } if (RuleDictionary.TryGetValue(GetKeyString(neighborhood), out int stateValue)) { return(Piece.Get(stateValue)); } else { return(piece); } }
public void RuleDictionaryTests(string input, string template, int generations, string[] expected) { var rules = RuleDictionary.Parse(input, default); var templateDictionary = rules.GetTemplateDictionary(); templateDictionary.ApplyTemplate(template); templateDictionary.Process(generations); var actual = from kvp in templateDictionary let pair = kvp.Key let count = kvp.Value from key in Enumerable.Repeat(pair, (int)count) orderby key select key; Assert.Equal(expected, actual); }