예제 #1
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            // Part 1
            long sumP1 = 0;

            foreach (string expression in inputList)
            {
                long result = Evaluate(expression);
                sumP1 += result;
            }

            Console.WriteLine(sumP1);

            // Part 2
            long sumP2 = 0;

            foreach (string expression in inputList)
            {
                long result = Evaluate(expression, true);
                sumP2 += result;
            }

            Console.WriteLine(sumP2);
            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();
            List <Seat>   seats     = inputList.ConvertAll(str => new Seat(str));

            seats = seats.OrderBy(seat => seat.id).ToList();

            int lastSeatId = 0;

            foreach (Seat seat in seats)
            {
                if (lastSeatId != 0 && seat.id != lastSeatId + 1)
                {
                    break;
                }
                else
                {
                    lastSeatId = seat.id;
                }
            }

            Console.WriteLine(seats.Last().id);
            Console.WriteLine(lastSeatId + 1);
            Console.ReadLine();
        }
예제 #3
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            width  = inputList[0].Length;
            height = inputList.Count;

            map = new GridType[height][];
            for (int height_i = 0; height_i < height; height_i++)
            {
                map[height_i] = new GridType[width];
                for (int width_i = 0; width_i < width; width_i++)
                {
                    if (inputList[height_i][width_i] == '.')
                    {
                        map[height_i][width_i] = GridType.Open;
                    }
                    else
                    {
                        map[height_i][width_i] = GridType.Tree;
                    }
                }
            }

            int trees_1_1 = CountTrees(1, 1);
            int trees_1_3 = CountTrees(1, 3);
            int trees_1_5 = CountTrees(1, 5);
            int trees_1_7 = CountTrees(1, 7);
            int trees_2_1 = CountTrees(2, 1);

            Console.WriteLine(trees_1_3);
            Console.WriteLine(trees_1_1 * trees_1_3 * trees_1_5 * trees_1_7 * trees_2_1);
            Console.ReadLine();
        }
예제 #4
0
        static void Main(string[] args)
        {
            List <string>   inputList             = AoCUtilities.GetInputLines();
            List <Password> passwords             = inputList.ConvertAll(str => new Password(str));
            int             validP1PasswordsCount = passwords.Count(password => password.validP1);
            int             validP2PasswordsCount = passwords.Count(password => password.validP2);

            Console.WriteLine(validP1PasswordsCount);
            Console.WriteLine(validP2PasswordsCount);
            Console.ReadLine();
        }
예제 #5
0
        static void Main(string[] args)
        {
            List <string> inputList             = AoCUtilities.GetInputLines();
            int           earliestDepartureTime = int.Parse(inputList[0]);

            string[]   busStringParts = inputList[1].Split(',');
            List <Bus> buses          = new List <Bus>();

            foreach (string busStringPart in busStringParts)
            {
                buses.Add(new Bus(busStringPart));
            }

            List <Bus> busesByEarliest          = buses.OrderBy(bus => bus.FindFirstDepartureAfter(earliestDepartureTime)).ToList();
            Bus        earliestBus              = busesByEarliest[0];
            int        earliestBusDepartureTime = earliestBus.FindFirstDepartureAfter(earliestDepartureTime);
            int        answer = earliestBus.TimePeriod * (earliestBusDepartureTime - earliestDepartureTime);

            Console.WriteLine(answer);

            bool       foundSolution = false;
            List <int> busTimesFound = new List <int>();
            long       trialTime     = 1;
            long       timeIncrement = 1;

            while (!foundSolution)
            {
                foundSolution = true;

                for (int bus_i = 0; bus_i < buses.Count; bus_i++)
                {
                    Bus bus = buses[bus_i];
                    if (bus.TimePeriod != 0)
                    {
                        long requiredDepartureTime = trialTime + bus_i;
                        if (requiredDepartureTime % bus.TimePeriod != 0)
                        {
                            foundSolution = false;
                            break;
                        }
                        else if (!busTimesFound.Contains(bus.TimePeriod))
                        {
                            busTimesFound.Add(bus.TimePeriod);
                            timeIncrement = LCM(busTimesFound);
                        }
                    }
                }
                trialTime += timeIncrement;
            }
            trialTime -= timeIncrement;

            Console.WriteLine(trialTime);
            Console.ReadLine();
        }
예제 #6
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            foreach (string bagRuleString in inputList)
            {
                string[] bagRuleStringParts = bagRuleString.Split(new string[] { " bags contain " }, StringSplitOptions.RemoveEmptyEntries);
                string   bagColour          = bagRuleStringParts[0];
                if (bags.ContainsKey(bagColour))
                {
                    throw new ArgumentException("Rule already exists for this bag");
                }
                bags[bagColour] = new Bag(bagColour, bagRuleStringParts[1]);
            }

            List <Bag> bagsThatWouldAllowShinyGoldBag = new List <Bag>();
            List <Bag> bagsToCheckForThatWouldBeValid = new List <Bag> {
                bags["shiny gold"]
            };

            while (bagsToCheckForThatWouldBeValid.Count > 0)
            {
                List <Bag> nextBagsToCheckForThatWouldBeValid = new List <Bag>();
                foreach (Bag bagToCheckForThatWouldBeValid in bagsToCheckForThatWouldBeValid)
                {
                    foreach (KeyValuePair <string, Bag> kv in bags)
                    {
                        if (kv.Value.allowedBags.ContainsKey(bagToCheckForThatWouldBeValid.colour))
                        {
                            if (!bagsThatWouldAllowShinyGoldBag.Contains(kv.Value))
                            {
                                bagsThatWouldAllowShinyGoldBag.Add(kv.Value);
                            }
                            nextBagsToCheckForThatWouldBeValid.Add(kv.Value);
                        }
                    }
                }
                bagsToCheckForThatWouldBeValid = nextBagsToCheckForThatWouldBeValid;
            }

            int bagsInsideShinyGoldCount = bags["shiny gold"].BagsInsideThisCount();

            Console.WriteLine(bagsThatWouldAllowShinyGoldBag.Count);
            Console.WriteLine(bagsInsideShinyGoldCount);
            Console.ReadLine();
        }
예제 #7
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            int acc;

            Simulate(inputList, out acc);
            Console.WriteLine(acc);

            bool loop = true;

            for (int instruction_i = 0; instruction_i < inputList.Count; instruction_i++)
            {
                string instruction = inputList[instruction_i];
                string opcode      = instruction.Substring(0, 3);
                if (opcode == "nop" || opcode == "jmp")
                {
                    List <string> copiedInputList = new List <string>(inputList);
                    if (opcode == "nop")
                    {
                        copiedInputList[instruction_i] = instruction.Replace("nop", "jmp");
                    }
                    else if (opcode == "jmp")
                    {
                        copiedInputList[instruction_i] = instruction.Replace("jmp", "nop");
                    }
                    loop = Simulate(copiedInputList, out acc);
                    if (!loop)
                    {
                        break;
                    }
                }
            }

            if (loop)
            {
                throw new ArgumentException("Didn't find an instruction to switch that fixes the error");
            }

            Console.WriteLine(acc);
            Console.ReadLine();
        }
예제 #8
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            foreach (var entry1 in inputList)
            {
                int val1 = int.Parse(entry1);
                foreach (var entry2 in inputList)
                {
                    int val2 = int.Parse(entry2);
                    foreach (var entry3 in inputList)
                    {
                        int val3 = int.Parse(entry3);
                        if (val1 + val2 + val3 == 2020)
                        {
                            Console.WriteLine(val1 * val2 * val3);
                        }
                    }
                }
            }

            Console.ReadLine();
        }
예제 #9
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            int cardPublicKey = int.Parse(inputList[0]);
            int doorPublicKey = int.Parse(inputList[1]);

            //cardPublicKey = 5764801;
            //doorPublicKey = 17807724;

            int cardLoopSize = FindLoopSize(cardPublicKey, 7);
            int doorLoopSize = FindLoopSize(doorPublicKey, 7);

            long encryptionKey1 = Transform(cardLoopSize, doorPublicKey);
            long encryptionKey2 = Transform(doorLoopSize, cardPublicKey);

            if (encryptionKey1 != encryptionKey2)
            {
                throw new ArgumentException("Invalid input");
            }

            Console.WriteLine(encryptionKey1);
            Console.ReadLine();
        }
예제 #10
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            List <Food> foods = new List <Food>();
            Dictionary <string, Allergen>   allAllergens   = new Dictionary <string, Allergen>();
            Dictionary <string, Ingredient> allIngredients = new Dictionary <string, Ingredient>();

            foreach (string foodString in inputList)
            {
                foods.Add(new Food(allAllergens, allIngredients, foodString));
            }

            foreach (var allergenKV in allAllergens)
            {
                Allergen allergen = allergenKV.Value;

                List <Ingredient> intersection = null;

                foreach (Food foodWithThisAllergen in allergen.foodsWithThisAllergen)
                {
                    if (intersection == null)
                    {
                        intersection = foodWithThisAllergen.ingredients;
                    }
                    else
                    {
                        intersection = intersection.Intersect(foodWithThisAllergen.ingredients).ToList();
                    }
                }

                allergen.possibleIngredients = intersection;
            }

            bool changeMade = true;

            while (changeMade)
            {
                changeMade = false;

                foreach (var allergenKV in allAllergens)
                {
                    Allergen allergen = allergenKV.Value;
                    if (allergen.possibleIngredients.Count == 1 && allergen.knownIngredient == null)
                    {
                        changeMade = true;

                        allergen.knownIngredient = allergen.possibleIngredients[0];
                        allergen.knownIngredient.knownToCauseThisAllergen = allergen;

                        foreach (var otherAllergenKV in allAllergens)
                        {
                            Allergen otherAllergen = otherAllergenKV.Value;
                            if (allergen != otherAllergen)
                            {
                                otherAllergen.possibleIngredients.Remove(allergen.knownIngredient);
                            }
                        }
                    }
                }
            }

            List <Ingredient> safeIngredients = allIngredients.Values.ToList();

            foreach (var allergenKV in allAllergens)
            {
                Allergen allergen = allergenKV.Value;
                foreach (var possibleIngredient in allergen.possibleIngredients)
                {
                    safeIngredients.Remove(possibleIngredient);
                }
            }

            int numberOfSafeIngredients = 0;

            foreach (Food food in foods)
            {
                foreach (Ingredient ingredient in safeIngredients)
                {
                    if (food.ingredients.Contains(ingredient))
                    {
                        numberOfSafeIngredients++;
                    }
                }
            }

            Console.WriteLine(numberOfSafeIngredients);

            List <Allergen> allAllergensList = allAllergens.Values.OrderBy(allergen => allergen.name).ToList();
            string          canonicalDangerousIngredientList = string.Join(",", allAllergensList.ConvertAll(allergen => allergen.knownIngredient.name));

            Console.WriteLine(canonicalDangerousIngredientList);

            Console.ReadLine();
        }
예제 #11
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            Tile referenceTile = Tile.Create(0, 0);

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

            foreach (string tileIdentifierString in inputList)
            {
                Tile currentTile = referenceTile;
                for (int char_i = 0; char_i < tileIdentifierString.Length; char_i++)
                {
                    string direction = "";
                    direction += tileIdentifierString[char_i];
                    if (direction == "s" || direction == "n")
                    {
                        direction += tileIdentifierString[char_i + 1];
                        char_i++;
                    }

                    switch (direction)
                    {
                    case "e":
                        currentTile = currentTile.e;
                        break;

                    case "w":
                        currentTile = currentTile.w;
                        break;

                    case "se":
                        currentTile = currentTile.se;
                        break;

                    case "sw":
                        currentTile = currentTile.sw;
                        break;

                    case "ne":
                        currentTile = currentTile.ne;
                        break;

                    case "nw":
                        currentTile = currentTile.nw;
                        break;
                    }
                }
                if (!currentTile.white)
                {
                }
                currentTile.white = !currentTile.white;
            }

            int blackTileCount = 0;

            foreach (var temp in TilesNEE.Values)
            {
                foreach (var tile in temp.Values)
                {
                    if (!tile.white)
                    {
                        blackTileCount++;
                    }
                }
            }

            Console.WriteLine(blackTileCount);

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

            for (int day = 0; day < 100; day++)
            {
                List <Tile> tilesToGenerateBorderFor = new List <Tile>();
                foreach (var temp in TilesNEE.Values)
                {
                    foreach (var tile in temp.Values)
                    {
                        tilesToGenerateBorderFor.Add(tile);
                    }
                }
                foreach (Tile tile in tilesToGenerateBorderFor)
                {
                    Tile t;
                    t = tile.e;
                    t = tile.w;
                    t = tile.ne;
                    t = tile.sw;
                    t = tile.nw;
                    t = tile.se;
                }

                List <Tile> tilesToCheckForFlip = new List <Tile>();
                foreach (var temp in TilesNEE.Values)
                {
                    foreach (var tile in temp.Values)
                    {
                        tilesToCheckForFlip.Add(tile);
                    }
                }
                List <Tile> tilesToFlip = new List <Tile>();
                foreach (Tile tile in tilesToCheckForFlip)
                {
                    int adjacentBlackTiles = 0;
                    if (!tile.e.white)
                    {
                        adjacentBlackTiles++;
                    }
                    if (!tile.w.white)
                    {
                        adjacentBlackTiles++;
                    }
                    if (!tile.ne.white)
                    {
                        adjacentBlackTiles++;
                    }
                    if (!tile.sw.white)
                    {
                        adjacentBlackTiles++;
                    }
                    if (!tile.nw.white)
                    {
                        adjacentBlackTiles++;
                    }
                    if (!tile.se.white)
                    {
                        adjacentBlackTiles++;
                    }
                    if (tile.white && adjacentBlackTiles == 2)
                    {
                        tilesToFlip.Add(tile);
                    }
                    else if (!tile.white && (adjacentBlackTiles == 0 || adjacentBlackTiles > 2))
                    {
                        tilesToFlip.Add(tile);
                    }
                }
                foreach (Tile tile in tilesToFlip)
                {
                    tile.white = !tile.white;
                }
            }

            blackTileCount = 0;
            foreach (var temp in TilesNEE.Values)
            {
                foreach (var tile in temp.Values)
                {
                    if (!tile.white)
                    {
                        blackTileCount++;
                    }
                }
            }
            Console.WriteLine(blackTileCount);
            Console.ReadLine();
        }
예제 #12
0
        static void Main(string[] args)
        {
            int positionX      = 0;
            int positionY      = 0;
            int angleFromNorth = 90;

            List <string> inputList = AoCUtilities.GetInputLines();

            foreach (string instruction in inputList)
            {
                int numericArg = int.Parse(instruction.Substring(1));
                switch (instruction[0])
                {
                case 'N':
                    positionY += numericArg;
                    break;

                case 'S':
                    positionY -= numericArg;
                    break;

                case 'E':
                    positionX += numericArg;
                    break;

                case 'W':
                    positionX -= numericArg;
                    break;

                case 'R':
                    angleFromNorth += numericArg;
                    angleFromNorth  = angleFromNorth % 360;
                    if (angleFromNorth < 0)
                    {
                        angleFromNorth += 360;
                    }
                    break;

                case 'L':
                    angleFromNorth -= numericArg;
                    angleFromNorth  = angleFromNorth % 360;
                    if (angleFromNorth < 0)
                    {
                        angleFromNorth += 360;
                    }
                    break;

                case 'F':
                    positionX += numericArg * (int)Math.Round(Math.Sin(angleFromNorth * (Math.PI / 180)));
                    positionY += numericArg * (int)Math.Round(Math.Cos(angleFromNorth * (Math.PI / 180)));
                    break;
                }
            }

            Console.WriteLine(Math.Abs(positionX) + Math.Abs(positionY));

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

            positionX = 0;
            positionY = 0;
            int waypointOffsetX = 10;
            int waypointOffsetY = 1;

            foreach (string instruction in inputList)
            {
                int numericArg = int.Parse(instruction.Substring(1));
                switch (instruction[0])
                {
                case 'N':
                    waypointOffsetY += numericArg;
                    break;

                case 'S':
                    waypointOffsetY -= numericArg;
                    break;

                case 'E':
                    waypointOffsetX += numericArg;
                    break;

                case 'W':
                    waypointOffsetX -= numericArg;
                    break;

                case 'R':
                {
                    int temp;
                    if (numericArg == 90)
                    {
                        temp            = waypointOffsetY;
                        waypointOffsetY = -waypointOffsetX;
                        waypointOffsetX = temp;
                    }
                    else if (numericArg == 180)
                    {
                        waypointOffsetY = -waypointOffsetY;
                        waypointOffsetX = -waypointOffsetX;
                    }
                    else if (numericArg == 270)
                    {
                        temp            = waypointOffsetY;
                        waypointOffsetY = waypointOffsetX;
                        waypointOffsetX = -temp;
                    }
                }
                break;

                case 'L':
                {
                    int temp;
                    if (numericArg == 270)
                    {
                        temp            = waypointOffsetY;
                        waypointOffsetY = -waypointOffsetX;
                        waypointOffsetX = temp;
                    }
                    else if (numericArg == 180)
                    {
                        waypointOffsetY = -waypointOffsetY;
                        waypointOffsetX = -waypointOffsetX;
                    }
                    else if (numericArg == 90)
                    {
                        temp            = waypointOffsetY;
                        waypointOffsetY = waypointOffsetX;
                        waypointOffsetX = -temp;
                    }
                }
                break;

                case 'F':
                    positionX += numericArg * waypointOffsetX;
                    positionY += numericArg * waypointOffsetY;
                    break;
                }
            }

            Console.WriteLine(Math.Abs(positionX) + Math.Abs(positionY));
            Console.ReadLine();
        }
예제 #13
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

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

            const int totalCupsP1 = 9;

            LinkedListNode[] nodesByValue = new LinkedListNode[totalCupsP1 + 1];
            LinkedListNode   previousNode = null;
            LinkedListNode   firstNode    = null;

            foreach (char c in inputList[0])
            {
                int            value   = (int)char.GetNumericValue(c);
                LinkedListNode newNode = new LinkedListNode(value);
                nodesByValue[value] = newNode;
                if (firstNode == null)
                {
                    firstNode = newNode;
                }
                if (previousNode != null)
                {
                    newNode.previous  = previousNode;
                    previousNode.next = newNode;
                }
                previousNode = newNode;
            }

            firstNode.previous = previousNode;
            previousNode.next  = firstNode;

            LinkedListNode currentCup = firstNode;

            Sim(totalCupsP1, 100, currentCup, nodesByValue);

            LinkedListNode oneNode = nodesByValue[1];

            Print(oneNode.next, 8);

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

            const int totalCupsP2 = 1000000;

            nodesByValue = new LinkedListNode[totalCupsP2 + 1];
            previousNode = null;
            firstNode    = null;

            foreach (char c in inputList[0])
            {
                long           value   = (long)char.GetNumericValue(c);
                LinkedListNode newNode = new LinkedListNode(value);
                nodesByValue[value] = newNode;
                if (firstNode == null)
                {
                    firstNode = newNode;
                }
                if (previousNode != null)
                {
                    newNode.previous  = previousNode;
                    previousNode.next = newNode;
                }
                previousNode = newNode;
            }

            for (long i = 10; i <= totalCupsP2; i++)
            {
                LinkedListNode newNode = new LinkedListNode(i);
                nodesByValue[i]   = newNode;
                newNode.previous  = previousNode;
                previousNode.next = newNode;
                previousNode      = newNode;
            }

            firstNode.previous = previousNode;
            previousNode.next  = firstNode;

            currentCup = firstNode;

            Sim(totalCupsP2, 10000000, currentCup, nodesByValue);

            oneNode = nodesByValue[1];

            long product = oneNode.next.value * oneNode.next.next.value;

            Console.WriteLine(product);

            Console.ReadLine();
        }
예제 #14
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();
            ulong         pow_2_36  = (ulong)Math.Pow(2, 36);

            Dictionary <ulong, ulong> memoryP1 = new Dictionary <ulong, ulong>();

            ulong maxValue36bit = pow_2_36 - 1;
            ulong andMask       = maxValue36bit;
            ulong orMask        = 0;

            foreach (string line in inputList)
            {
                string[] lineParts = line.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries);
                if (lineParts[0] == "mask")
                {
                    andMask = maxValue36bit;
                    orMask  = 0;
                    for (int i = 0; i < 36; i++)
                    {
                        char character = lineParts[1][i];
                        switch (character)
                        {
                        case '0':
                        {
                            andMask &= ~((ulong)1 << (35 - i));
                        }
                        break;

                        case '1':
                        {
                            orMask |= (ulong)1 << (35 - i);
                        }
                        break;
                        }
                    }
                }
                else
                {
                    string addressString = lineParts[0].Substring(4, lineParts[0].Length - 5);
                    ulong  address       = ulong.Parse(addressString);
                    ulong  preMaskData   = ulong.Parse(lineParts[1]);
                    ulong  postMaskData  = (preMaskData | orMask) & andMask;
                    memoryP1[address] = postMaskData;
                }
            }

            ulong totalP1 = 0;

            foreach (var kv in memoryP1)
            {
                totalP1 += kv.Value;
            }
            Console.WriteLine(totalP1);

            ///////////////////////////////////////
            // Part 2
            ///////////////////////////////////////
            ///
            Dictionary <ulong, ulong> memoryP2 = new Dictionary <ulong, ulong>();

            string maskString = "";

            foreach (string line in inputList)
            {
                string[] lineParts = line.Split(new string[] { " = " }, StringSplitOptions.RemoveEmptyEntries);
                if (lineParts[0] == "mask")
                {
                    maskString = lineParts[1];
                }
                else
                {
                    string       addressString     = lineParts[0].Substring(4, lineParts[0].Length - 5);
                    ulong        address           = ulong.Parse(addressString);
                    ulong        data              = ulong.Parse(lineParts[1]);
                    List <ulong> possibleAddresses = GetPotentialAddressesFrom(address, maskString);
                    foreach (ulong possibleAddress in possibleAddresses)
                    {
                        memoryP2[possibleAddress] = data;
                    }
                }
            }

            ulong totalP2 = 0;

            foreach (var kv in memoryP2)
            {
                totalP2 += kv.Value;
            }
            Console.WriteLine(totalP2);
            Console.ReadLine();
        }
예제 #15
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();
            List <Field>  fields    = new List <Field>();
            int           i         = 0;
            string        line      = inputList[i];

            while (line != "")
            {
                var    parts1 = line.Split(new string[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
                string name   = parts1[0];
                var    field  = new Field(name);

                var parts2 = parts1[1].Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string requirementString in parts2)
                {
                    var requirementStringParts = requirementString.Split('-');
                    int min = int.Parse(requirementStringParts[0]);
                    int max = int.Parse(requirementStringParts[1]);
                    field.requirements.Add(new Requirement(min, max));
                }

                fields.Add(field);

                i++;
                line = inputList[i];
            }

            i += 2;
            string yourTicketString = inputList[i];
            Ticket yourTicket       = new Ticket(yourTicketString);

            List <Ticket> otherTickets = new List <Ticket>();

            i += 3;
            while (i < inputList.Count)
            {
                line = inputList[i];

                otherTickets.Add(new Ticket(line));

                i++;
            }

            List <Ticket> validOtherTickets = new List <Ticket>();

            int sumP1 = 0;

            foreach (Ticket otherTicket in otherTickets)
            {
                bool allValuesValid = true;
                foreach (int value in otherTicket.values)
                {
                    bool valid = false;
                    foreach (Field field in fields)
                    {
                        foreach (Requirement requirement in field.requirements)
                        {
                            if (value >= requirement.min && value <= requirement.max)
                            {
                                valid = true;
                                break;
                            }
                        }
                        if (valid)
                        {
                            break;
                        }
                    }
                    if (!valid)
                    {
                        sumP1         += value;
                        allValuesValid = false;
                    }
                }
                otherTicket.valid = allValuesValid;
                if (allValuesValid)
                {
                    validOtherTickets.Add(otherTicket);
                }
            }

            Console.WriteLine(sumP1);

            foreach (Field field in fields)
            {
                for (int x = 0; x < yourTicket.values.Count; x++)
                {
                    field.possiblePositions.Add(x);
                }
            }

            foreach (Field field in fields)
            {
                for (int possiblePosition_i = 0; possiblePosition_i < field.possiblePositions.Count;)
                {
                    int possiblePosition = field.possiblePositions[possiblePosition_i];

                    bool positionValid = true;
                    foreach (Ticket ticket in validOtherTickets)
                    {
                        int  trialValue = ticket.values[possiblePosition];
                        bool valid      = false;
                        foreach (Requirement requirement in field.requirements)
                        {
                            if (trialValue >= requirement.min && trialValue <= requirement.max)
                            {
                                valid = true;
                                break;
                            }
                        }
                        if (!valid)
                        {
                            positionValid = false;
                        }
                    }

                    if (!positionValid)
                    {
                        field.possiblePositions.RemoveAt(possiblePosition_i);
                    }
                    else
                    {
                        possiblePosition_i++;
                    }
                }
            }

            bool changeMade = true;

            while (changeMade)
            {
                changeMade = false;

                foreach (Field field in fields)
                {
                    if (field.position == -1 && field.possiblePositions.Count == 1)
                    {
                        int knownPositionForThisField = field.possiblePositions[0];
                        field.position = knownPositionForThisField;
                        // remove this position from other fields
                        foreach (Field otherField in fields)
                        {
                            if (field != otherField)
                            {
                                changeMade = true;
                                otherField.possiblePositions.Remove(knownPositionForThisField);
                            }
                        }
                    }
                }
            }

            bool foundSolution = true;
            long product       = 1;

            foreach (Field field in fields)
            {
                if (field.position == -1)
                {
                    foundSolution = false;
                    break;
                }

                if (field.name.Length >= 9 && field.name.Substring(0, 9) == "departure")
                {
                    product *= yourTicket.values[field.position];
                }
            }

            if (!foundSolution)
            {
                throw new Exception("No solution found");
            }

            Console.WriteLine(product);
            Console.ReadLine();
        }
예제 #16
0
        static void Main(string[] args)
        {
            Dictionary <int, List <Turn> > numbersTurns = new Dictionary <int, List <Turn> >();
            Turn lastTurn = null;

            List <string> inputList = AoCUtilities.GetInputLines();

            string[] turnStrings = inputList[0].Split(',');
            int      turn_i      = 1;

            for (; turn_i <= turnStrings.Length; turn_i++)
            {
                string turnString = turnStrings[turn_i - 1];
                int    number     = int.Parse(turnString);
                Turn   turn       = new Turn(turn_i, number);
                if (!numbersTurns.ContainsKey(number))
                {
                    numbersTurns[number] = new List <Turn> {
                        turn
                    };
                }
                else
                {
                    numbersTurns[number].Add(turn);
                }
                lastTurn = turn;
            }

            for (; turn_i <= 30000000; turn_i++)
            {
                int         lastNumberSpoke     = lastTurn.number;
                List <Turn> turnsWithThatNumber = numbersTurns[lastNumberSpoke];
                int         nextNumber;
                if (turnsWithThatNumber.Count > 1)
                {
                    int lastTurnThatNumberSpoken       = lastTurn.id;
                    int secondLastTurnThatNumberSpoken = turnsWithThatNumber[turnsWithThatNumber.Count - 2].id;
                    nextNumber = lastTurnThatNumberSpoken - secondLastTurnThatNumberSpoken;
                }
                else
                {
                    nextNumber = 0;
                }

                if (turn_i == 2020)
                {
                    Console.WriteLine(nextNumber);
                }

                Turn newTurn = new Turn(turn_i, nextNumber);
                if (!numbersTurns.ContainsKey(nextNumber))
                {
                    numbersTurns[nextNumber] = new List <Turn> {
                        newTurn
                    };
                }
                else
                {
                    numbersTurns[nextNumber].Add(newTurn);
                }
                lastTurn = newTurn;
            }

            numbersTurns.Clear();
            Console.WriteLine(lastTurn.number);
            Console.ReadLine();
        }
예제 #17
0
 static void Main(string[] args)
 {
     List <string> inputList = AoCUtilities.GetInputLines();
 }
예제 #18
0
        static void Main(string[] args)
        {
            List <string> inputList        = AoCUtilities.GetInputLines();
            List <long>   inputNumbers     = inputList.ConvertAll(str => long.Parse(str));
            const int     preambleLength   = 25;
            List <long>   availableNumbers = new List <long>(preambleLength);

            for (int i = 0; i < preambleLength; i++)
            {
                availableNumbers.Add(inputNumbers[i]);
            }

            int number_i = preambleLength;

            for (; number_i < inputNumbers.Count; number_i++)
            {
                long targetNumber = inputNumbers[number_i];
                bool foundSum     = false;
                foreach (long availableNumber1 in availableNumbers)
                {
                    foreach (long availableNumber2 in availableNumbers)
                    {
                        if (availableNumber1 + availableNumber2 == targetNumber)
                        {
                            foundSum = true;
                            break;
                        }
                    }
                    if (foundSum)
                    {
                        break;
                    }
                }
                if (foundSum)
                {
                    availableNumbers.RemoveAt(0);
                    availableNumbers.Add(targetNumber);
                }
                else
                {
                    break;
                }
            }

            long badNumber = inputNumbers[number_i];

            Console.WriteLine(badNumber);

            List <long> contiguousRange = null;

            for (int startNumber_i = 0; startNumber_i < inputNumbers.Count - 1; startNumber_i++)
            {
                long sum = 0;
                int  contiguousElementCount = 2;
                while (sum < badNumber)
                {
                    sum = inputNumbers.Skip(startNumber_i).Take(contiguousElementCount).Sum();
                    contiguousElementCount++;
                }
                if (sum == badNumber)
                {
                    contiguousRange = inputNumbers.Skip(startNumber_i).Take(contiguousElementCount - 1).ToList();
                }
            }
            if (contiguousRange == null)
            {
                throw new ArgumentException("No contiguous range is found that sums to the value " + badNumber);
            }
            long minInContiguousRange = contiguousRange.Min();
            long maxInContiguousRange = contiguousRange.Max();
            long weakness             = minInContiguousRange + maxInContiguousRange;

            Console.WriteLine(weakness);
            Console.ReadLine();
        }
예제 #19
0
        static void Main(string[] args)
        {
            Dictionary <int, int> joltageDifferences = new Dictionary <int, int>();

            List <string> inputList         = AoCUtilities.GetInputLines();
            List <int>    availableAdapters = inputList.ConvertAll(str => int.Parse(str));

            List <int> sortedAdapters = availableAdapters.OrderBy(adapter => adapter).ToList();

            joltageDifferences[3] = 1;
            for (int adapter_i = 0; adapter_i < sortedAdapters.Count; adapter_i++)
            {
                int adapterJoltage = sortedAdapters[adapter_i];
                int joltageDifference;
                if (adapter_i == 0)
                {
                    joltageDifference = adapterJoltage;
                }
                else
                {
                    joltageDifference = adapterJoltage - sortedAdapters[adapter_i - 1];
                }

                if (joltageDifferences.ContainsKey(joltageDifference))
                {
                    joltageDifferences[joltageDifference]++;
                }
                else
                {
                    joltageDifferences[joltageDifference] = 1;
                }
            }

            Console.WriteLine(joltageDifferences[1] * joltageDifferences[3]);


            Dictionary <int, Node> adapterNodes = new Dictionary <int, Node>();

            Node outletNode = new Node(0);

            adapterNodes[0]        = outletNode;
            outletNode.WaysToReach = 1;

            int  highestJoltageAdapter = sortedAdapters.Max();
            Node deviceNode            = new Node(highestJoltageAdapter + 3);

            adapterNodes[highestJoltageAdapter + 3] = deviceNode;

            for (int adapter_i = 0; adapter_i < sortedAdapters.Count; adapter_i++)
            {
                int adapterJoltage = sortedAdapters[adapter_i];
                adapterNodes[adapterJoltage] = new Node(adapterJoltage);
            }

            outletNode.FindNodesInReach(adapterNodes);
            for (int adapter_i = 0; adapter_i < sortedAdapters.Count; adapter_i++)
            {
                int adapterJoltage = sortedAdapters[adapter_i];
                adapterNodes[adapterJoltage].FindNodesInReach(adapterNodes);
            }

            Console.WriteLine(deviceNode.WaysToReach);
            Console.ReadLine();
        }
예제 #20
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            /*
             *   y
             *   ^
             *   |
             * z x---> x
             *
             *
             */
            // Planes : Rows : Cube

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

            for (int y = 0; y < inputList.Count; y++)
            {
                string rowString = inputList[y];
                for (int x = 0; x < rowString.Length; x++)
                {
                    char cubeChar = rowString[x];

                    Coord coord = new Coord(x, y, 0);
                    switch (cubeChar)
                    {
                    case '.':
                        setCube(x, y, 0, new Cube(x, y, 0, false));
                        break;

                    case '#':
                        setCube(x, y, 0, new Cube(x, y, 0, true));
                        break;
                    }
                }
            }

            //print();

            for (int cycle = 0; cycle < 6; cycle++)
            {
                int _minZ = minZ;
                int _maxZ = maxZ;
                int _minY = minY;
                int _maxY = maxY;
                int _minX = minX;
                int _maxX = maxX;
                for (int z = _minZ - 1; z <= _maxZ + 1; z++)
                {
                    for (int y = _minY - 1; y <= _maxY + 1; y++)
                    {
                        for (int x = _minX - 1; x <= _maxX + 1; x++)
                        {
                            Cube        cube                    = getCube(x, y, z);
                            List <Cube> neighbourCubes          = getNeighbourCubes(x, y, z);
                            int         howManyActiveNeighbours = neighbourCubes.Count(c => c.active);

                            if (cube.active)
                            {
                                cube.newActive = howManyActiveNeighbours == 2 || howManyActiveNeighbours == 3;
                            }
                            else
                            {
                                cube.newActive = howManyActiveNeighbours == 3;
                            }
                        }
                    }
                }

                foreach (var twoDimWorldKV in threeDimWorld)
                {
                    foreach (var oneDimWorldKV in twoDimWorldKV.Value)
                    {
                        foreach (var cubeKV in oneDimWorldKV.Value)
                        {
                            cubeKV.Value.active = cubeKV.Value.newActive;
                        }
                    }
                }

                //Console.WriteLine($"Cycle {cycle + 1}");
                //print();
            }

            int activeCountP1 = 0;

            foreach (var twoDimWorldKV in threeDimWorld)
            {
                foreach (var oneDimWorldKV in twoDimWorldKV.Value)
                {
                    foreach (var cubeKV in oneDimWorldKV.Value)
                    {
                        if (cubeKV.Value.active)
                        {
                            activeCountP1++;
                        }
                    }
                }
            }
            Console.WriteLine(activeCountP1);

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

            minX = int.MaxValue;
            maxX = int.MinValue;
            minY = int.MaxValue;
            maxY = int.MinValue;
            minZ = int.MaxValue;
            maxZ = int.MinValue;
            minW = int.MaxValue;
            maxW = int.MinValue;

            for (int y = 0; y < inputList.Count; y++)
            {
                string rowString = inputList[y];
                for (int x = 0; x < rowString.Length; x++)
                {
                    char cubeChar = rowString[x];

                    Coord coord = new Coord(x, y, 0);
                    switch (cubeChar)
                    {
                    case '.':
                        setCube(x, y, 0, 0, new Cube(x, y, 0, 0, false));
                        break;

                    case '#':
                        setCube(x, y, 0, 0, new Cube(x, y, 0, 0, true));
                        break;
                    }
                }
            }

            //print4d();

            for (int cycle = 0; cycle < 6; cycle++)
            {
                int _minW = minW;
                int _maxW = maxW;
                int _minZ = minZ;
                int _maxZ = maxZ;
                int _minY = minY;
                int _maxY = maxY;
                int _minX = minX;
                int _maxX = maxX;
                for (int w = _minW - 1; w <= _maxW + 1; w++)
                {
                    for (int z = _minZ - 1; z <= _maxZ + 1; z++)
                    {
                        for (int y = _minY - 1; y <= _maxY + 1; y++)
                        {
                            for (int x = _minX - 1; x <= _maxX + 1; x++)
                            {
                                Cube        cube                    = getCube(x, y, z, w);
                                List <Cube> neighbourCubes          = getNeighbourCubes(x, y, z, w);
                                int         howManyActiveNeighbours = neighbourCubes.Count(c => c.active);

                                if (cube.active)
                                {
                                    cube.newActive = howManyActiveNeighbours == 2 || howManyActiveNeighbours == 3;
                                }
                                else
                                {
                                    cube.newActive = howManyActiveNeighbours == 3;
                                }
                            }
                        }
                    }
                }

                foreach (var threeDimWorldKV in fourDimWorld)
                {
                    foreach (var twoDimWorldKV in threeDimWorldKV.Value)
                    {
                        foreach (var oneDimWorldKV in twoDimWorldKV.Value)
                        {
                            foreach (var cubeKV in oneDimWorldKV.Value)
                            {
                                cubeKV.Value.active = cubeKV.Value.newActive;
                            }
                        }
                    }
                }

                //Console.WriteLine($"Cycle {cycle + 1}");
                //print4d();
            }

            int activeCountP2 = 0;

            foreach (var threeDimWorldKV in fourDimWorld)
            {
                foreach (var twoDimWorldKV in threeDimWorldKV.Value)
                {
                    foreach (var oneDimWorldKV in twoDimWorldKV.Value)
                    {
                        foreach (var cubeKV in oneDimWorldKV.Value)
                        {
                            if (cubeKV.Value.active)
                            {
                                activeCountP2++;
                            }
                        }
                    }
                }
            }
            Console.WriteLine(activeCountP2);

            Console.ReadLine();
        }
예제 #21
0
        static void Main(string[] args)
        {
            List <string> inputList = AoCUtilities.GetInputLines();

            height = inputList.Count;
            width  = inputList[0].Length;
            grid   = new GridCell[height][];
            for (int y = 0; y < height; y++)
            {
                grid[y] = new GridCell[width];
                for (int x = 0; x < width; x++)
                {
                    switch (inputList[y][x])
                    {
                    case 'L':
                        grid[y][x] = GridCell.AvailableChair;
                        break;

                    case '.':
                        grid[y][x] = GridCell.Empty;
                        break;

                    default:
                        throw new ArgumentException("Expecting only L and .");
                    }
                }
            }

            bool changeMade = true;

            while (changeMade)
            {
                changeMade = false;
                GridCell[][] copyOfGrid = new GridCell[height][];
                for (int y = 0; y < height; y++)
                {
                    copyOfGrid[y] = new GridCell[width];
                    for (int x = 0; x < width; x++)
                    {
                        copyOfGrid[y][x] = grid[y][x];
                    }
                }


                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (grid[y][x] != GridCell.Empty)
                        {
                            int occupiedAdjacentSeats = 0;
                            // left
                            if (x > 0 && grid[y][x - 1] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // right
                            if (x < width - 1 && grid[y][x + 1] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // top
                            if (y > 0 && grid[y - 1][x] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // bottom
                            if (y < height - 1 && grid[y + 1][x] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // tl
                            if (x > 0 && y > 0 && grid[y - 1][x - 1] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // bl
                            if (x > 0 && y < height - 1 && grid[y + 1][x - 1] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // tr
                            if (x < width - 1 && y > 0 && grid[y - 1][x + 1] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // br
                            if (x < width - 1 && y < height - 1 && grid[y + 1][x + 1] == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }

                            if (grid[y][x] == GridCell.AvailableChair)
                            {
                                if (occupiedAdjacentSeats == 0)
                                {
                                    changeMade       = true;
                                    copyOfGrid[y][x] = GridCell.OccupiedChair;
                                }
                            }
                            else if (grid[y][x] == GridCell.OccupiedChair)
                            {
                                if (occupiedAdjacentSeats >= 4)
                                {
                                    changeMade       = true;
                                    copyOfGrid[y][x] = GridCell.AvailableChair;
                                }
                            }
                        }
                    }
                }

                grid = copyOfGrid;
            }

            int occupiedSeatCount = 0;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (grid[y][x] == GridCell.OccupiedChair)
                    {
                        occupiedSeatCount++;
                    }
                }
            }
            Console.WriteLine(occupiedSeatCount);


            //////////////////////////////////////
            // PART 2
            //////////////////////////////////////

            grid = new GridCell[height][];
            for (int y = 0; y < height; y++)
            {
                grid[y] = new GridCell[width];
                for (int x = 0; x < width; x++)
                {
                    switch (inputList[y][x])
                    {
                    case 'L':
                        grid[y][x] = GridCell.AvailableChair;
                        break;

                    case '.':
                        grid[y][x] = GridCell.Empty;
                        break;

                    default:
                        throw new ArgumentException("Expecting only L and .");
                    }
                }
            }

            changeMade = true;
            while (changeMade)
            {
                changeMade = false;
                GridCell[][] copyOfGrid = new GridCell[height][];
                for (int y = 0; y < height; y++)
                {
                    copyOfGrid[y] = new GridCell[width];
                    for (int x = 0; x < width; x++)
                    {
                        copyOfGrid[y][x] = grid[y][x];
                    }
                }
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        if (grid[y][x] != GridCell.Empty)
                        {
                            int occupiedAdjacentSeats = 0;
                            // left
                            if (FindFirstChair(x, y, -1, 0) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // right
                            if (FindFirstChair(x, y, +1, 0) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // top
                            if (FindFirstChair(x, y, 0, -1) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // bottom
                            if (FindFirstChair(x, y, 0, +1) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // tl
                            if (FindFirstChair(x, y, -1, -1) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // bl
                            if (FindFirstChair(x, y, -1, +1) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // tr
                            if (FindFirstChair(x, y, +1, -1) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }
                            // br
                            if (FindFirstChair(x, y, +1, +1) == GridCell.OccupiedChair)
                            {
                                occupiedAdjacentSeats++;
                            }

                            if (grid[y][x] == GridCell.AvailableChair)
                            {
                                if (occupiedAdjacentSeats == 0)
                                {
                                    changeMade       = true;
                                    copyOfGrid[y][x] = GridCell.OccupiedChair;
                                }
                            }
                            else if (grid[y][x] == GridCell.OccupiedChair)
                            {
                                if (occupiedAdjacentSeats >= 5)
                                {
                                    changeMade       = true;
                                    copyOfGrid[y][x] = GridCell.AvailableChair;
                                }
                            }
                        }
                    }
                }

                grid = copyOfGrid;
            }

            occupiedSeatCount = 0;
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (grid[y][x] == GridCell.OccupiedChair)
                    {
                        occupiedSeatCount++;
                    }
                }
            }
            Console.WriteLine(occupiedSeatCount);

            Console.ReadLine();
        }