Program.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. namespace D04._1
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. if (args.Length < 1) throw new ArgumentException();
  11. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  12. int answer = 0;
  13. using (var file = File.OpenText(args[0]))
  14. {
  15. while (true)
  16. {
  17. var line = file.ReadLine();
  18. if (line == null) break;
  19. int lio = line.LastIndexOf('-'), io = line.IndexOf('[');
  20. var name = line.Substring(0, lio).Replace("-", "");
  21. var hash = line.Substring(io + 1).TrimEnd(']');
  22. var id = int.Parse(line.Substring(lio + 1, io - lio - 1));
  23. int[] frequencies = new int['z' - 'a' + 1];
  24. foreach (char c in name) frequencies[c - 'a']++;
  25. var letters = string.Join("", frequencies.Select((f, index) => (((char)(index + 'a'), f) as (char letter, int freq)?).Value)
  26. .OrderByDescending(kvp => kvp.freq)
  27. .ThenBy(kvp => kvp.letter)
  28. .Take(5)
  29. .Select(kvp => kvp.letter));
  30. if (letters == hash) answer += id;
  31. }
  32. }
  33. Console.WriteLine($"The answer is : {answer}");
  34. }
  35. }
  36. }