Example #1
0
        static void Main(string[] args)
        {
            string          input               = AoCUtilities.GetInput();
            List <string>   passportStrings     = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None).ToList();
            List <Passport> passports           = passportStrings.ConvertAll(str => new Passport(str));
            int             validPassportsCount = passports.Count(passport => passport.valid);

            Console.WriteLine(validPassportsCount);
            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            string        input        = AoCUtilities.GetInput();
            List <string> groupStrings = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            List <Group>  groups       = groupStrings.ConvertAll(str => new Group(str));

            int summedUniqueAnswers = 0;

            foreach (Group group in groups)
            {
                summedUniqueAnswers += group.uniqueAnswerCount;
            }
            int summedEveryoneAnsweredYes = 0;

            foreach (Group group in groups)
            {
                summedEveryoneAnsweredYes += group.everyoneAnsweredYesCount;
            }

            Console.WriteLine(summedUniqueAnswers);
            Console.WriteLine(summedEveryoneAnsweredYes);
            Console.ReadLine();
        }
Example #3
0
        static void Main(string[] args)
        {
            string input = AoCUtilities.GetInput();


            string[]   playerDecks   = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            List <int> originalDeck1 = new List <int>();
            List <int> originalDeck2 = new List <int>();

            List <int> currentDeck;

            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    currentDeck = originalDeck1;
                }
                else
                {
                    currentDeck = originalDeck2;
                }

                var deckStringParts = playerDecks[i].Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 1; j < deckStringParts.Length; j++)
                {
                    var cardString = deckStringParts[j];
                    currentDeck.Add(int.Parse(cardString));
                }
            }

            //////////////////////////////////////
            // Part 1
            //////////////////////////////////////

            List <int>    deck1  = new List <int>(originalDeck1);
            List <int>    deck2  = new List <int>(originalDeck2);
            List <string> hashes = new List <string>()
            {
                string.Join("", deck1)
            };

            List <int> winningDeck = null;

            while (winningDeck == null && deck1.Count > 0 && deck2.Count > 0)
            {
                int nextDeck1 = deck1[0];
                int nextDeck2 = deck2[0];
                deck1.RemoveAt(0);
                deck2.RemoveAt(0);
                if (nextDeck1 > nextDeck2)
                {
                    deck1.Add(nextDeck1);
                    deck1.Add(nextDeck2);
                }
                else
                {
                    deck2.Add(nextDeck2);
                    deck2.Add(nextDeck1);
                }
                string newHash = string.Join("", deck1);
                if (hashes.Contains(newHash))
                {
                    winningDeck = deck1;
                }
                else
                {
                    hashes.Add(newHash);
                }
            }

            if (winningDeck == null)
            {
                if (deck1.Count == 0)
                {
                    winningDeck = deck2;
                }
                else
                {
                    winningDeck = deck1;
                }
            }

            int score = 0;

            for (int i = 1; i <= winningDeck.Count; i++)
            {
                int value = winningDeck[winningDeck.Count - i];
                score += value * i;
            }

            Console.WriteLine(score);

            //////////////////////////////////////
            // Part 2
            //////////////////////////////////////

            deck1 = new List <int>(originalDeck1);
            deck2 = new List <int>(originalDeck2);

            bool player1Wins = RecursiveGame(deck1, deck2);

            if (player1Wins)
            {
                winningDeck = deck1;
            }
            else
            {
                winningDeck = deck2;
            }

            score = 0;
            for (int i = 1; i <= winningDeck.Count; i++)
            {
                int value = winningDeck[winningDeck.Count - i];
                score += value * i;
            }

            Console.WriteLine(score);

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            string input = AoCUtilities.GetInput();

            /////////////////////
            // Part 1
            /////////////////////

            List <Tile> tilesR0          = new List <Tile>();
            List <Tile> tilesR90         = new List <Tile>();
            List <Tile> tilesR180        = new List <Tile>();
            List <Tile> tilesR270        = new List <Tile>();
            List <Tile> flippedTilesR0   = new List <Tile>();
            List <Tile> flippedTilesR90  = new List <Tile>();
            List <Tile> flippedTilesR180 = new List <Tile>();
            List <Tile> flippedTilesR270 = new List <Tile>();

            var tileStrings = input.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var tileString in tileStrings)
            {
                var    tileLines = tileString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                string idString  = tileLines[0].Substring(5, tileLines[0].Length - 6);
                int    id        = int.Parse(idString);

                Tile newTileR0 = new Tile(id, tileLines);
                tilesR0.Add(newTileR0);
                Tile newTileR90 = newTileR0.GetR90Tile();
                tilesR90.Add(newTileR90);
                Tile newTileR180 = newTileR90.GetR90Tile();
                tilesR180.Add(newTileR180);
                Tile newTileR270 = newTileR180.GetR90Tile();
                tilesR270.Add(newTileR270);
                Tile flippedNewTileR0 = newTileR0.GetFlippedTile();
                flippedTilesR0.Add(flippedNewTileR0);
                Tile flippedNewTileR90 = flippedNewTileR0.GetR90Tile();
                flippedTilesR90.Add(flippedNewTileR90);
                Tile flippedNewTileR180 = flippedNewTileR90.GetR90Tile();
                flippedTilesR180.Add(flippedNewTileR180);
                Tile flippedNewTileR270 = flippedNewTileR180.GetR90Tile();
                flippedTilesR270.Add(flippedNewTileR270);
            }


            Tile        anchorTile = tilesR0[0];
            List <Tile> usedTiles  = new List <Tile> {
                anchorTile
            };

            anchorTile.FindMatch(usedTiles, tilesR0, tilesR90, tilesR180, tilesR270, flippedTilesR0, flippedTilesR90, flippedTilesR180, flippedTilesR270);

            long product = 1;
            Tile tlTile  = null;

            foreach (Tile usedTile in usedTiles)
            {
                if (usedTile.TL)
                {
                    tlTile = usedTile;
                }
                if (usedTile.TL || usedTile.TR || usedTile.BL || usedTile.BR)
                {
                    product *= usedTile.id;
                }
            }
            Console.WriteLine(product);

            /////////////////////
            // Part 2
            /////////////////////

            int tilesDim = (int)Math.Sqrt(usedTiles.Count);

            Tile[][] tileMap = new Tile[tilesDim][];

            int  tile_y     = 0;
            Tile bottomTile = tlTile;

            while (bottomTile != null)
            {
                tileMap[tile_y] = new Tile[tilesDim];

                int  tile_x    = 0;
                Tile rightTile = bottomTile;
                while (rightTile != null)
                {
                    tileMap[tile_y][tile_x] = rightTile;
                    tile_x++;
                    rightTile = rightTile.rightTile;
                }
                tile_y++;
                bottomTile = bottomTile.bottomTile;
            }

            int   imageDim = tilesDim * 8;
            Image imageR0  = new Image(imageDim);

            int whitePixelCount = 0;

            for (tile_y = 0; tile_y < tilesDim; tile_y++)
            {
                for (int tile_x = 0; tile_x < tilesDim; tile_x++)
                {
                    Tile tile = tileMap[tile_y][tile_x];
                    for (int pixel_y = 1; pixel_y <= 8; pixel_y++)
                    {
                        for (int pixel_x = 1; pixel_x <= 8; pixel_x++)
                        {
                            int globalPixel_y = (tile_y * 8) + (pixel_y - 1);
                            int globalPixel_x = (tile_x * 8) + (pixel_x - 1);
                            imageR0.pixels[globalPixel_y][globalPixel_x] = tile.pixels[pixel_y][pixel_x];
                            if (tile.pixels[pixel_y][pixel_x].type == PixelType.white)
                            {
                                whitePixelCount++;
                            }
                        }
                    }
                }
            }

            Image imageR90         = imageR0.GetR90Image();
            Image imageR180        = imageR90.GetR90Image();
            Image imageR270        = imageR180.GetR90Image();
            Image imageFlippedR0   = imageR0.GetFlippedImage();
            Image imageFlippedR90  = imageFlippedR0.GetR90Image();
            Image imageFlippedR180 = imageFlippedR90.GetR90Image();
            Image imageFlippedR270 = imageFlippedR180.GetR90Image();

            //imageR0.print();
            int seaMonsters = 0;

            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageR0);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageR90);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageR180);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageR270);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageFlippedR0);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageFlippedR90);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageFlippedR180);
            }
            if (seaMonsters == 0)
            {
                seaMonsters = searchForSeaMonsters(imageFlippedR270);
            }

            int nonSeaMonstersWhitePixels = whitePixelCount - (seaMonsters * 15);

            Console.WriteLine(nonSeaMonstersWhitePixels);
            Console.ReadLine();
        }
Example #5
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();
        }