| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace D16._1
- {
- 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> thingInLetter = new Dictionary<string, int>
- {
- { "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<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> thingInLetter, HashSet<Sue> 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<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);
- }
- }
- }
- }
- }
|