示例#1
0
        private void LoadData()
        {
            string inputFile = AppDomain.CurrentDomain.BaseDirectory + @"..\..\input.txt";

            if (File.Exists(inputFile))
            {
                string        line;
                StreamReader  file      = new StreamReader(inputFile);
                string[]      nameArray = { "children", "cats", "samoyeds", "pomeranians", "akitas", "vizslas", "goldfish", "trees", "cars", "perfumes" };
                List <string> names     = new List <string>(nameArray);
                List <int>    values    = new List <int>(new int[10]);
                while ((line = file.ReadLine()) != null)
                {
                    line = line.Replace(' ', ':').Replace(',', ':');
                    string[] tokens = line.Split(':');
                    Aunt     aunt   = new Aunt(int.Parse(tokens[1]));
                    int      token  = 3;
                    while (token <= 13)
                    {
                        aunt.Attributes[tokens[token]] = int.Parse(tokens[token + 2]);
                        token += 4;
                    }
                    _aunts.Add(aunt);
                }

                file.Close();
            }
        }
示例#2
0
文件: Program.cs 项目: Zeydel/AoC15
        static void Main(string[] args)
        {
            string path = @"..\..\input.txt";

            string[] inputArray = System.IO.File.ReadAllLines(path);

            Aunt giftGiver = new Aunt
            {
                Properties = new Dictionary <string, int>()
            };

            giftGiver.Properties.Add("children", 3);
            giftGiver.Properties.Add("cats", 7);
            giftGiver.Properties.Add("samoyeds", 2);
            giftGiver.Properties.Add("pomeranians", 3);
            giftGiver.Properties.Add("akitas", 0);
            giftGiver.Properties.Add("vizslas", 0);
            giftGiver.Properties.Add("goldfish", 5);
            giftGiver.Properties.Add("trees", 3);
            giftGiver.Properties.Add("cars", 2);
            giftGiver.Properties.Add("perfumes", 1);

            List <Aunt> aunts = new List <Aunt>();

            foreach (string str in inputArray)
            {
                string   normalized  = Regex.Replace(str, "[^a-zA-Z0-9 ]", "");
                string[] splitString = normalized.Split(' ');
                int      number      = int.Parse(splitString[1]);
                Aunt     newAunt     = new Aunt
                {
                    Number     = number,
                    Properties = new Dictionary <string, int>()
                };

                for (int i = 2; i < splitString.Length; i += 2)
                {
                    newAunt.Properties.Add(splitString[i], int.Parse(splitString[i + 1]));
                }

                aunts.Add(newAunt);
            }

            foreach (Aunt aunt in aunts)
            {
                bool rightAunt = true;
                foreach (string key in aunt.Properties.Keys)
                {
                    if (key.Equals("cats") || key.Equals("trees"))
                    {
                        if (!(giftGiver.Properties[key] < aunt.Properties[key]))
                        {
                            rightAunt = false;
                            break;
                        }
                    }
                    else if (key.Equals("pomeranians") || key.Equals("goldfish"))
                    {
                        if (!(giftGiver.Properties[key] > aunt.Properties[key]))
                        {
                            rightAunt = false;
                            break;
                        }
                    }
                    else
                    {
                        if (giftGiver.Properties[key] != aunt.Properties[key])
                        {
                            rightAunt = false;
                            break;
                        }
                    }
                }

                if (rightAunt)
                {
                    Console.WriteLine(aunt.Number);
                    break;
                }
            }

            Console.ReadKey();
        }