using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace D16._2 { 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, "eq") }, { "cats", (7, "gt") }, { "samoyeds", (2, "eq") }, { "pomeranians", (3, "lt") }, { "akitas", (0, "eq") }, { "vizslas", (0, "eq") }, { "goldfish", (5, "lt") }, { "trees", (3, "gt") }, { "cars", (2, "eq") }, { "perfumes", (1, "eq") } }; 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 ((string key, (int value, string op) thing) in thingInLetter) { if (sue.Things.ContainsKey(key) == false) continue; if ((thing.op == "gt" && sue.Things[key] <= thing.value) || (thing.op == "lt" && sue.Things[key] >= thing.value) || (thing.op == "eq" && sue.Things[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); } } } } }