Esempio n. 1
0
        public void print()
        {
#if DEBUG || OVERRIDE
            for (int y = 0; y < this.dim; y++)
            {
                for (int x = 0; x < this.dim; x++)
                {
                    AoCUtilities.DebugWrite("{0}", (char)this.pixels[y][x].type);
                }
                AoCUtilities.DebugWriteLine();
            }
            AoCUtilities.DebugWriteLine();
#endif
        }
Esempio n. 2
0
        // This doesn't work, was an attempt to avoid using regex
        public int CheckMessage(string messageSubstring, int recursion = 0)
        {
            //for (int i = 0; i < recursion; i++)
            //    Console.Write("\t");
            //Console.WriteLine($"{this.id} {messageSubstring}");
            int lengthOfMatch = 0;

            if (this.leaf)
            {
                if (messageSubstring.Length > 0 && messageSubstring[0] == this.value)
                {
                    lengthOfMatch = 1;
                }
            }
            else
            {
                bool matchStr;
                foreach (StringOfRules strOfRules in this.alternatives)
                {
                    for (int i = 0; i < recursion; i++)
                    {
                        AoCUtilities.DebugWrite("\t");
                    }
                    AoCUtilities.DebugWriteLine($"{messageSubstring} must match {strOfRules}");
                    lengthOfMatch = 0;
                    matchStr      = true;
                    foreach (Rule rule in strOfRules.rules)
                    {
                        for (int i = 0; i < recursion; i++)
                        {
                            AoCUtilities.DebugWrite("\t");
                        }
                        AoCUtilities.DebugWriteLine($"{rule}");
                        string messageSubstringSection = messageSubstring.Substring(lengthOfMatch);
                        int    lengthOfSubMatch        = rule.CheckMessage(messageSubstringSection, recursion + 1);
                        if (lengthOfSubMatch == 0)
                        {
                            matchStr = false;
                            break;
                        }
                        else
                        {
                            lengthOfMatch += lengthOfSubMatch;
                        }
                    }
                    if (matchStr)
                    {
                        for (int i = 0; i < recursion; i++)
                        {
                            AoCUtilities.DebugWrite("\t");
                        }
                        AoCUtilities.DebugWriteLine($"True");
                        break;
                    }
                    else
                    {
                        lengthOfMatch = 0;
                        for (int i = 0; i < recursion; i++)
                        {
                            AoCUtilities.DebugWrite("\t");
                        }
                        AoCUtilities.DebugWriteLine($"False");
                    }
                }
            }
            return(lengthOfMatch);
        }
Esempio n. 3
0
        static bool RecursiveGame(List <int> deck1, List <int> deck2, int recursionLevel = 1)
        {
            List <string> hashes = new List <string>()
            {
                string.Join("", deck1)
            };

            int round = 1;

            AoCUtilities.DebugWriteLine();
            AoCUtilities.DebugWriteLine($"=== Game Recursion Level {recursionLevel} ===");

            while (deck1.Count > 0 && deck2.Count > 0)
            {
                AoCUtilities.DebugWriteLine($"- Round {round} (Game {recursionLevel}) -");
                AoCUtilities.DebugWriteLine($"Player 1's deck: {string.Join(", ", deck1)}");
                AoCUtilities.DebugWriteLine($"Player 2's deck: {string.Join(", ", deck2)}");
                int nextDeck1 = deck1[0];
                int nextDeck2 = deck2[0];
                deck1.RemoveAt(0);
                deck2.RemoveAt(0);
                AoCUtilities.DebugWriteLine($"Player 1 plays: {nextDeck1}");
                AoCUtilities.DebugWriteLine($"Player 2 plays: {nextDeck2}");

                if (deck1.Count >= nextDeck1 && deck2.Count >= nextDeck2)
                {
                    AoCUtilities.DebugWriteLine($"Playing a sub-game to determine the winner...");
                    // recurse
                    List <int> deck1Copy   = new List <int>(deck1.GetRange(0, nextDeck1));
                    List <int> deck2Copy   = new List <int>(deck2.GetRange(0, nextDeck2));
                    bool       player1Wins = RecursiveGame(deck1Copy, deck2Copy, recursionLevel + 1);
                    if (player1Wins)
                    {
                        deck1.Add(nextDeck1);
                        deck1.Add(nextDeck2);
                        AoCUtilities.DebugWriteLine($"Player 1 wins round {round} of game {recursionLevel}!");
                    }
                    else
                    {
                        deck2.Add(nextDeck2);
                        deck2.Add(nextDeck1);
                        AoCUtilities.DebugWriteLine($"Player 2 wins round {round} of game {recursionLevel}!");
                    }
                }
                else
                {
                    if (nextDeck1 > nextDeck2)
                    {
                        deck1.Add(nextDeck1);
                        deck1.Add(nextDeck2);
                        AoCUtilities.DebugWriteLine($"Player 1 wins round {round} of game {recursionLevel}!");
                    }
                    else
                    {
                        deck2.Add(nextDeck2);
                        deck2.Add(nextDeck1);
                        AoCUtilities.DebugWriteLine($"Player 2 wins round {round} of game {recursionLevel}!");
                    }
                }

                string newHash = string.Join(",", deck1) + "|" + string.Join(",", deck2);
                if (hashes.Contains(newHash))
                {
                    AoCUtilities.DebugWriteLine($"Back to game level {recursionLevel - 1}...");
                    return(true);
                }
                else
                {
                    hashes.Add(newHash);
                }

                round++;
                AoCUtilities.DebugWriteLine();
            }

            AoCUtilities.DebugWriteLine($"Back to game level {recursionLevel-1}...");
            return(deck2.Count == 0);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Dictionary <int, Rule> rules = new Dictionary <int, Rule>();

            string input       = AoCUtilities.GetInput();
            var    inputParts  = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string rulesString = inputParts[0];

            string[] ruleStrings = rulesString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            string   messages    = inputParts[1];

            string[] messageStrings = messages.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string ruleString in ruleStrings)
            {
                var ruleStringParts = ruleString.Split(':');
                int id = int.Parse(ruleStringParts[0]);

                Rule newRule;
                if (rules.ContainsKey(id))
                {
                    newRule = rules[id];
                }
                else
                {
                    newRule    = new Rule();
                    newRule.id = id;
                    rules[id]  = newRule;
                }

                if (ruleStringParts[1][1] == '"')
                {
                    newRule.leaf  = true;
                    newRule.value = ruleStringParts[1][2];
                }
                else
                {
                    var ruleStringAlternatives = ruleStringParts[1].Split('|');
                    foreach (string ruleStringAlternative in ruleStringAlternatives)
                    {
                        string[]      subRuleStrings = ruleStringAlternative.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        StringOfRules stringOfRules  = new StringOfRules();
                        foreach (string subRuleString in subRuleStrings)
                        {
                            int  subRuleId = int.Parse(subRuleString);
                            Rule subRule;
                            if (rules.ContainsKey(subRuleId))
                            {
                                subRule = rules[subRuleId];
                            }
                            else
                            {
                                subRule          = new Rule();
                                subRule.id       = subRuleId;
                                rules[subRuleId] = subRule;
                            }
                            stringOfRules.rules.Add(subRule);
                        }

                        newRule.alternatives.Add(stringOfRules);
                    }
                }
            }

            string regex          = rules[0].EvaluateRegex();
            string regexPatternP1 = $@"^\b{regex}\b$";

            AoCUtilities.DebugWriteLine("{0}", regexPatternP1.Length);
            AoCUtilities.DebugWriteLine(regexPatternP1);
            Regex regexP1 = new Regex(regexPatternP1);
            int   messagesMatchCountP1 = 0;

            foreach (string messageString in messageStrings)
            {
                bool match = regexP1.IsMatch(messageString);
                if (match)
                {
                    messagesMatchCountP1++;
                }
            }
            Console.WriteLine(messagesMatchCountP1);

            var new8strOfRules = new StringOfRules();

            new8strOfRules.rules.Add(rules[42]);
            new8strOfRules.rules.Add(rules[8]);
            rules[8].alternatives.Add(new8strOfRules);
            var new11strOfRules = new StringOfRules();

            new11strOfRules.rules.Add(rules[42]);
            new11strOfRules.rules.Add(rules[11]);
            new11strOfRules.rules.Add(rules[31]);
            rules[11].alternatives.Add(new11strOfRules);

            regex = rules[0].EvaluateRegex();
            string regexPatternP2 = $@"^\b{regex}\b$";

            AoCUtilities.DebugWriteLine("{0}", regexPatternP2.Length);
            AoCUtilities.DebugWriteLine(regexPatternP2);
            Regex regexP2 = new Regex(regexPatternP2);
            int   messagesMatchCountP2 = 0;

            foreach (string messageString in messageStrings)
            {
                bool match = regexP2.IsMatch(messageString);
                if (match)
                {
                    messagesMatchCountP2++;
                }
            }
            Console.WriteLine(messagesMatchCountP2);

            Console.ReadLine();
        }