| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using System;
- using System.IO;
- using System.Linq;
- namespace D04._1
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- int answer = 0;
- using (var file = File.OpenText(args[0]))
- {
- while (true)
- {
- var line = file.ReadLine();
- if (line == null) break;
- int lio = line.LastIndexOf('-'), io = line.IndexOf('[');
- var name = line.Substring(0, lio).Replace("-", "");
- var hash = line.Substring(io + 1).TrimEnd(']');
- var id = int.Parse(line.Substring(lio + 1, io - lio - 1));
- int[] frequencies = new int['z' - 'a' + 1];
- foreach (char c in name) frequencies[c - 'a']++;
- 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) answer += id;
- }
- }
- Console.WriteLine($"The answer is : {answer}");
- }
- }
- }
|