예제 #1
0
파일: Program.cs 프로젝트: spscah/aoc20
        public override void Update(IList <Node> nodes)
        {
            IList <string> children = Text.Split(" | ").ToList();

            _a = new AllNode(children[0]);
            _a.Update(nodes);
            _b = new AllNode(children[1]);
            _b.Update(nodes);
        }
예제 #2
0
파일: Program.cs 프로젝트: spscah/aoc20
        static int CountMatchingRules(IList <string> rules, bool part2)
        {
            string pattern = @"(\d+): (.*)$";
            Regex  re      = new Regex(pattern, RegexOptions.IgnoreCase);
            Match  m;

            IList <Node> nodes = new List <Node>();

            foreach (string rule in rules)
            {
                if (string.IsNullOrEmpty(rule))
                {
                    break;
                }
                nodes.Add(null);
            }
            // hack
            int extra = 43 - nodes.Count;

            for (; extra > 0; --extra)
            {
                nodes.Add(null);
            }

            bool ruleUpdating = true;
            int  valid        = 0;

            foreach (string rule in rules)
            {
                if (ruleUpdating)
                {
                    if (string.IsNullOrEmpty(rule))
                    {
                        foreach (Node n in nodes.Where(n => n != null))
                        {
                            n.Update(nodes);
                        }
                        ruleUpdating = false;
                        continue;
                    }

                    m = re.Match(rule);
                    if (m.Success)
                    {
                        int  index   = int.Parse(m.Groups[1].Value);
                        Node newNode = null;
                        if (index == 0 && part2)
                        {
                            newNode = new ZeroNode(m.Groups[2].Value);
                        }
                        else if (index == 8 && part2)
                        {
                            newNode = new LoopNode1(m.Groups[2].Value);
                        }
                        else if (index == 11 && part2)
                        {
                            newNode = new LoopNode2(m.Groups[2].Value);
                        }
                        else if (m.Groups[2].Value.Contains('|'))
                        {
                            newNode = new AnyNode(m.Groups[2].Value);
                        }
                        else if (m.Groups[2].Value.StartsWith('"'))
                        {
                            newNode = new LeafNode(m.Groups[2].Value);
                        }
                        else
                        {
                            newNode = new AllNode(m.Groups[2].Value);
                        }
                        nodes[index] = newNode;
                    }
                }
                else
                {
                    string copy = rule;
                    if (nodes[0].Matches(ref copy) && copy.Length == 0)
                    {
                        ++valid;
                    }
                }
            }
            return(valid);
        }