using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace D16._1 { public class Sue { public int Id { get; } public Dictionary Things = new Dictionary(); public Sue(int id) => Id = id; public override bool Equals(object obj) => obj is Sue sue && Id == sue.Id; public override int GetHashCode() => HashCode.Combine(Id); } class Program { static void Main(string[] args) { if (args.Length < 1) throw new ArgumentException(); if (File.Exists(args[0]) == false) throw new FileNotFoundException(); List sues = new List(); Dictionary thingInLetter = new Dictionary { { "children", 3 }, { "cats", 7 }, { "samoyeds", 2 }, { "pomeranians", 3 }, { "akitas", 0 }, { "vizslas", 0 }, { "goldfish", 5 }, { "trees", 3 }, { "cars", 2 }, { "perfumes", 1 } }; ParseFile(args[0], sues); HashSet possibleSues = new HashSet(); FindSue(sues, thingInLetter, possibleSues); if (possibleSues.Count > 1) throw new Exception(); Console.WriteLine($"I may send my card to Aunt Sue {possibleSues.First().Id}"); } private static void FindSue(List sues, Dictionary thingInLetter, HashSet possibleSues) { foreach (var sue in sues) { bool mayBeThatSue = true; foreach (var thing in thingInLetter) { if (sue.Things.ContainsKey(thing.Key) == false) continue; if (sue.Things[thing.Key] != thing.Value) { mayBeThatSue = false; break; } } if (mayBeThatSue) possibleSues.Add(sue); } } private static void ParseFile(string arg, List sues) { using (var file = File.OpenText(arg)) { while (true) { var line = file.ReadLine(); if (line == null) break; var lr = line.Split(": ", 2); var id = int.Parse(lr[0].Substring("Sue ".Length)); var sue = new Sue(id); foreach (var thing in lr[1].Split(", ")) { var thinglr = thing.Split(": "); int qt = int.Parse(thinglr[1]); sue.Things.Add(thinglr[0], qt); } sues.Add(sue); } } } } }