private static Dictionary <string, long> GrowPolymerCounts(PolymerInstructions polymerInstruction, Dictionary <string, long> pairCount) { Dictionary <string, long> newCounts = new Dictionary <string, long>(); foreach (var pair in pairCount.Keys) { var monomer = polymerInstruction.InsertionRules[pair]; var newPair1 = string.Concat(pair[0], monomer); var newPair2 = string.Concat(monomer, pair[1]); if (newCounts.ContainsKey(newPair1)) { newCounts[newPair1] += pairCount[pair]; } else { newCounts.Add(newPair1, pairCount[pair]); } if (newCounts.ContainsKey(newPair2)) { newCounts[newPair2] += pairCount[pair]; } else { newCounts.Add(newPair2, pairCount[pair]); } } return(newCounts); }
private static long RunCount(PolymerInstructions polymerInstruction, int steps) { var polymer = polymerInstruction.Template; var pairCount = new Dictionary <string, long>(); for (int i = 0; i < polymer.Length - 1; i++) { var p1 = polymer[i]; var p3 = polymer[i + 1]; string pair = string.Concat(p1, p3); if (pairCount.ContainsKey(pair)) { pairCount[pair] += 1; } else { pairCount.Add(pair, 1); } } for (int step = 0; step < steps; step++) { pairCount = GrowPolymerCounts(polymerInstruction, pairCount); } var monomerCounts = pairCount.SelectMany(p => p.Key.ToArray().Select(c => new { Key = c, Count = p.Value })) .GroupBy(a => a.Key).Select(grp => new { Key = grp.Key, Count = grp.Sum(g => g.Count) }).ToDictionary(k => k.Key, c => c.Count); monomerCounts[polymer.First()] += 1; monomerCounts[polymer.Last()] += 1; //All characters are counted twice now! var difference = (monomerCounts.Max(m => m.Value) - monomerCounts.Min(m => m.Value)) / 2; return(difference); }
private static string GrowPolymer(PolymerInstructions polymerInstruction, string polymer) { var sb = new StringBuilder(); sb.Append(polymer[0]); for (int i = 0; i < polymer.Length - 1; i++) { var p1 = polymer[i]; var p3 = polymer[i + 1]; string pair = string.Concat(p1, p3); var p2 = polymerInstruction.InsertionRules[pair]; sb.Append(p2); sb.Append(p3); } polymer = sb.ToString(); return(polymer); }
private static long RunPolymerization(PolymerInstructions polymerInstruction, int steps) { var polymer = polymerInstruction.Template; for (int step = 0; step < steps; step++) { polymer = GrowPolymer(polymerInstruction, polymer); } var monomerCounts = polymer.GroupBy(p => p).Select(p => new { Monomer = p.First(), Count = p.LongCount() }); var difference = monomerCounts.Max(m => m.Count) - monomerCounts.Min(m => m.Count); return(difference); }
public PolymerInstructions ParseFile(string filePath) { var instructions = new PolymerInstructions(); var mode = 0; foreach (var line in ReadData(filePath)) { if (string.IsNullOrWhiteSpace(line)) { mode++; continue; } if (mode == 0) { instructions.Template = line; } else if (mode == 1) { instructions.InsertionRules.Add(line.Substring(0, 2), line[6]); } } return(instructions); }