| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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<string> TestMolecules(string input, List<(string, string)> replacements)
- {
- HashSet<string> molecules = new HashSet<string>();
- 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;
- }
- }
- }
|