コード例 #1
0
        static void Main(string[] args)
        {
            Test1();
            Test2();

            var contents = File.ReadAllText("input.txt");
            var info     = TicketInfo.Parse(contents);
            int p1       = info.ScanErrorRate();

            Console.WriteLine("Part 1 scan error rate = " + p1);

            info.DiscardInvalidTickets();
            var   descs   = info.FindTicketDesc();
            ulong product = 1;

            for (var i = 0; i < descs.Count; i++)
            {
                if (descs[i].StartsWith("departure"))
                {
                    product *= (ulong)info.MyTicket[i];
                }
            }

            Console.WriteLine("Part 2 product = " + product);
        }
コード例 #2
0
        static void Test1()
        {
            var sample = @"class: 1-3 or 5-7
                row: 6-11 or 33-44
                seat: 13-40 or 45-50

                your ticket:
                7,1,14

                nearby tickets:
                7,3,47
                40,4,50
                55,2,20
                38,6,12";

            var info = TicketInfo.Parse(sample);

            Debug.Assert(info.MyTicket.Count == 3);
            Debug.Assert(info.MyTicket[0] == 7);
            Debug.Assert(info.MyTicket[2] == 14);

            Debug.Assert(info.Rules.Count == 3);
            Debug.Assert(info.Rules[0].Name == "class");
            Debug.Assert(info.Rules[0].R1Min == 1);

            Debug.Assert(info.Others.Count == 4);

            Debug.Assert(info.ScanErrorRate() == 71);

            info.DiscardInvalidTickets();
            Debug.Assert(info.Others.Count == 1);
        }
コード例 #3
0
        static void Test2()
        {
            var sample = @"class: 0-1 or 4-19
                row: 0-5 or 8-19
                seat: 0-13 or 16-19

                your ticket:
                11,12,13

                nearby tickets:
                3,9,18
                15,1,5
                5,14,9";

            var info = TicketInfo.Parse(sample);

            info.DiscardInvalidTickets();
            var descs = info.FindTicketDesc();

            Debug.Assert(descs.Count == 3);
            Debug.Assert(descs[0] == "row");
            Debug.Assert(descs[1] == "class");
            Debug.Assert(descs[2] == "seat");
        }