コード例 #1
0
        private static void ParseRules(Dictionary <string, Bag> bags)
        {
            Parser <char, string>                       colorParser    = Any.AtLeastOnceUntil(Try(String(" bag")).Then(Char('s').Optional())).Map(s => new string(s.ToArray()));
            Parser <char, (string c, int n)>            bagCountParser = Num.Before(Char(' ')).Then(colorParser, (n, c) => (c, n));
            Parser <char, IEnumerable <(string, int)> > emptyParser    = String("no other bags").ThenReturn(Enumerable.Empty <(string, int)>());
            Parser <char, Bag> ruleParser = colorParser.Before(String(" contain ")).Then(emptyParser.Or(bagCountParser.Separated(String(", "))), (c, r) => CreateBag(bags, c, r)).Before(Char('.'));
            Parser <char, IEnumerable <Bag> > rulesParser = ruleParser.SeparatedAndOptionallyTerminated(Char('\n'));

            rulesParser.ParseOrThrow(File.ReadAllText("./input.txt"));
        }