| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace D04._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- using (var file = File.OpenText(args[0]))
- {
- var sb = new StringBuilder();
- while (true)
- {
- var line = file.ReadLine();
- if (line == null) break;
- int lio = line.LastIndexOf('-'), io = line.IndexOf('[');
- var name = line.Substring(0, lio);
- var hash = line.Substring(io + 1).TrimEnd(']');
- var id = int.Parse(line.Substring(lio + 1, io - lio - 1));
- var ALPHA = 'z' - 'a' + 1;
- sb.Clear();
- int[] frequencies = new int[ALPHA];
- foreach (char c in name)
- {
- if (c == '-')
- {
- sb.Append(' ');
- continue;
- }
- int lid = c - 'a';
- frequencies[lid]++;
- sb.Append((char)('a' + (lid + id) % ALPHA));
- }
- var letters = string.Join("", frequencies.Select((f, index) => (((char)(index + 'a'), f) as (char letter, int freq)?).Value)
- .OrderByDescending(kvp => kvp.freq)
- .ThenBy(kvp => kvp.letter)
- .Take(5)
- .Select(kvp => kvp.letter));
- if (letters == hash && sb.ToString().Contains("north"))
- Console.WriteLine($"The answer is : {id}");
- }
- }
- }
- }
- }
|