Program.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. namespace D04._2
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. if (args.Length < 1) throw new ArgumentException();
  12. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  13. using (var file = File.OpenText(args[0]))
  14. {
  15. var sb = new StringBuilder();
  16. while (true)
  17. {
  18. var line = file.ReadLine();
  19. if (line == null) break;
  20. int lio = line.LastIndexOf('-'), io = line.IndexOf('[');
  21. var name = line.Substring(0, lio);
  22. var hash = line.Substring(io + 1).TrimEnd(']');
  23. var id = int.Parse(line.Substring(lio + 1, io - lio - 1));
  24. var ALPHA = 'z' - 'a' + 1;
  25. sb.Clear();
  26. int[] frequencies = new int[ALPHA];
  27. foreach (char c in name)
  28. {
  29. if (c == '-')
  30. {
  31. sb.Append(' ');
  32. continue;
  33. }
  34. int lid = c - 'a';
  35. frequencies[lid]++;
  36. sb.Append((char)('a' + (lid + id) % ALPHA));
  37. }
  38. var letters = string.Join("", frequencies.Select((f, index) => (((char)(index + 'a'), f) as (char letter, int freq)?).Value)
  39. .OrderByDescending(kvp => kvp.freq)
  40. .ThenBy(kvp => kvp.letter)
  41. .Take(5)
  42. .Select(kvp => kvp.letter));
  43. if (letters == hash && sb.ToString().Contains("north"))
  44. Console.WriteLine($"The answer is : {id}");
  45. }
  46. }
  47. }
  48. }
  49. }