コード例 #1
0
        private void readRules(string listItem, List <ticketRule> tr)
        {
            ticketRule tic = new ticketRule();

            tic.name    = listItem.Split(':')[0];
            tic.val1min = int.Parse(listItem.Split(':')[1].Split(' ')[1].Split('-')[0]);
            tic.val1max = int.Parse(listItem.Split(':')[1].Split(' ')[1].Split('-')[1]);
            tic.val2min = int.Parse(listItem.Split(':')[1].Split(' ')[3].Split('-')[0]);
            tic.val2max = int.Parse(listItem.Split(':')[1].Split(' ')[3].Split('-')[1]);
            tr.Add(tic);
        }
コード例 #2
0
        private IEnumerable <int> Day1(string inData)
        {
            List <string>     list = inData.Split("\n").ToList();
            List <ticketRule> tr   = new List <ticketRule>();

            int result  = 0;
            int result2 = 0;
            int section = 1;

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] == "")
                {
                    section++;
                }

                if (section == 1)
                {
                    ticketRule tic = new ticketRule();
                    tic.name    = list[i].Split(':')[0];
                    tic.val1min = int.Parse(list[i].Split(':')[1].Split(' ')[1].Split('-')[0]);
                    tic.val1max = int.Parse(list[i].Split(':')[1].Split(' ')[1].Split('-')[1]);
                    tic.val2min = int.Parse(list[i].Split(':')[1].Split(' ')[3].Split('-')[0]);
                    tic.val2max = int.Parse(list[i].Split(':')[1].Split(' ')[3].Split('-')[1]);
                    tr.Add(tic);
                }

                if (section == 2)
                {
                    if (list[i] != "")
                    {
                        //do nothing for now
                    }
                }

                if (section == 3)
                {
                    if (list[i] != "")
                    {
                        if (!list[i].Contains("nearby tickets:"))
                        {
                            string[] values = list[i].Split(',');
                            foreach (string str in values)
                            {
                                bool isValid = false;

                                foreach (ticketRule ticket in tr)
                                {
                                    if (ticket.checkValues(int.Parse(str)))
                                    {
                                        isValid = true;
                                    }
                                }
                                if (!isValid)
                                {
                                    result += int.Parse(str);
                                    result2++;
                                }
                            }
                        }
                    }
                }
            }
            yield return(result);
        }