using System; using System.Collections.Generic; using System.IO; namespace D19._1 { public class Program { static void Main(string[] args) { if (args.Length < 1) throw new ArgumentException(); if (File.Exists(args[0]) == false) throw new FileNotFoundException(); string input; List<(string, string)> replacements; ParseFile(args, out input, out replacements); var molecules = TestMolecules(input, replacements); Console.WriteLine($"The answer is : {molecules.Count}"); } public static void ParseFile(string[] args, out string input, out List<(string, string)> replacements) { input = string.Empty; replacements = new List<(string, string)>(); using (var file = File.OpenText(args[0])) { while (true) { var line = file.ReadLine(); if (line == null) break; if (line == string.Empty) continue; var lr = line.Split(" => "); if (lr.Length == 2) replacements.Add((lr[0], lr[1])); else input = lr[0]; } } } public static HashSet TestMolecules(string input, List<(string, string)> replacements) { HashSet molecules = new HashSet(); foreach ((string orig, string replace) in replacements) { int index = 0; while (true) { index = input.IndexOf(orig, index); if (index == -1) break; string molecule = input.Substring(0, index) + replace + input.Substring(index + orig.Length); molecules.Add(molecule); index++; } } return molecules; } } }