コード例 #1
0
        public static long GetInvalidTicketCount(TicketRuleGraph graph)
        {
            var sum = 0;

            foreach (var nearbyTicket in graph.NearbyTickets)
            {
                foreach (var nearbyTicketValue in nearbyTicket.Values)
                {
                    var valid = false;
                    foreach (var graphTicketRule in graph.TicketRules)
                    {
                        foreach (var ticketRuleValue in graphTicketRule.Values)
                        {
                            if (nearbyTicketValue >= ticketRuleValue.Minimum && nearbyTicketValue <= ticketRuleValue.Maximum)
                            {
                                valid = true;
                                break;
                            }
                        }

                        if (valid)
                        {
                            break;
                        }
                    }

                    if (!valid)
                    {
                        sum += nearbyTicketValue;
                    }
                }
            }

            return(sum);
        }
コード例 #2
0
        public static TicketRuleGraph GetTicketRuleGraph(List <string> input)
        {
            var graph = new TicketRuleGraph {
                YourTicket = new Ticket(), TicketRules = new List <TicketRule>(), NearbyTickets = new List <Ticket>()
            };

            var count = 1;
            var skipYourTicketLine   = true;
            var skipNearbyTicketLine = true;

            foreach (var line in input)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    count++;
                    continue;
                }
                if (count == 1)
                {
                    // ticket rules
                    var ticketRule = new TicketRule {
                        Values = new List <TicketRuleValue>()
                    };

                    var colonSplit = line.Split(':');
                    ticketRule.Name = colonSplit[0].Trim();

                    var orSplit = Regex.Split(colonSplit[1].Trim(), " or ");

                    foreach (var orItem in orSplit)
                    {
                        var dashSplit = orItem.Split('-');

                        ticketRule.Values.Add(new TicketRuleValue {
                            Minimum = int.Parse(dashSplit[0]), Maximum = int.Parse(dashSplit[1])
                        });
                    }

                    graph.TicketRules.Add(ticketRule);
                }
                else if (count == 2)
                {
                    // your ticket
                    if (skipYourTicketLine)
                    {
                        skipYourTicketLine = false;
                        continue;
                    }

                    graph.YourTicket.Values = new List <int>();
                    graph.YourTicket.Values = line.Split(',').Select(int.Parse).ToList();
                }
                else
                {
                    // nearby tickets
                    if (skipNearbyTicketLine)
                    {
                        skipNearbyTicketLine = false;
                        continue;
                    }

                    graph.NearbyTickets.Add(new Ticket {
                        Values = line.Split(',').Select(int.Parse).ToList()
                    });
                }
            }

            return(graph);
        }