| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace D16._2
- {
- public class Sue
- {
- public int Id { get; }
- public Dictionary<string, int> Things = new Dictionary<string, int>();
- 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<Sue> sues = new List<Sue>();
- Dictionary<string, (int, string)> thingInLetter = new Dictionary<string, (int, string)>
- {
- { "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<Sue> possibleSues = new HashSet<Sue>();
- 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<Sue> sues, Dictionary<string, (int, string)> thingInLetter, HashSet<Sue> 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<Sue> 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);
- }
- }
- }
- }
- }
|