Пример #1
0
        public static void PartOne()
        {
            Console.WriteLine("Day Ten - Part One");
            List <int> adapters = PuzzleInputHelper.GetInputLines("DayTen.txt")
                                  .Select(Int32.Parse).OrderBy(i => i).ToList();

            Console.WriteLine($"There are {adapters.Count} adapters");

            Console.WriteLine("Adapters in order:");
            Console.WriteLine(String.Join(", ", adapters));

            int oneDiff   = 1;
            int threeDiff = 1;

            for (int i = 0; i < adapters.Count; i++)
            {
                if (i > 0)
                {
                    if (adapters[i] - adapters[i - 1] == 1)
                    {
                        oneDiff++;
                    }
                    else if (adapters[i] - adapters[i - 1] == 3)
                    {
                        threeDiff++;
                    }
                }
            }
            Console.WriteLine("Product of 1-differences and 3-differences: " + oneDiff * threeDiff);

            Console.WriteLine("Day Ten - End of Part One");
        }
Пример #2
0
        public static void PartOne()
        {
            Console.WriteLine("Day seven - Part One");
            List <BagRule> rules = PuzzleInputHelper.GetInputLines("DaySeven_Solene.txt")
                                   .Select(BagRule.FromLine)
                                   .ToList();

            Console.WriteLine($"There are {rules.Count} rules");
            List <string>  countedBags = new List <string>();
            Queue <string> bagsToCheck = new Queue <string>();

            bagsToCheck.Enqueue("shiny gold");

            while (bagsToCheck.Count > 0)
            {
                string currentBagToCheck = bagsToCheck.Dequeue();
                countedBags.Add(currentBagToCheck);
                var bagsAroundCurrent = rules.Where(br => br.CanContain(currentBagToCheck)).ToList();
                bagsAroundCurrent.ForEach(bag => { if (!countedBags.Contains(bag.Color) && !bagsToCheck.Contains(bag.Color))
                                                   {
                                                       bagsToCheck.Enqueue(bag.Color);
                                                   }
                                          });
            }
            Console.WriteLine($"There were {countedBags.Count} counted bags");
            Console.WriteLine("Day seven - End of part One");
        }
Пример #3
0
        public static void PartTwo()
        {
            Console.WriteLine("Day Two - Part Two");
            List <PasswordWithPolicy> passwords = PuzzleInputHelper.GetInputLines("DayTwo.txt")
                                                  .Select(PasswordWithPolicy.FromLine).ToList();
            int validPasswords = passwords.Count(p => p.SatisfiesNewPolicy());

            Console.WriteLine($"There are {validPasswords} valid passwords under new policy");
            Console.WriteLine("Day Two - End of part Two");
        }
Пример #4
0
        public static void PartOne()
        {
            Console.WriteLine("Day eight - Part One");
            OpcodeInstruction[] opcodes = PuzzleInputHelper.GetInputLines("DayEight.txt")
                                          .Select(OpcodeInstruction.FromLine).ToArray();
            OpcodeMachine opm = new OpcodeMachine(opcodes);

            opm.Run();
            Console.WriteLine("Day eight - End of part One");
        }
Пример #5
0
        public static void PartOne()
        {
            Console.WriteLine("Day one - Part One");
            List <int> lines = PuzzleInputHelper.GetInputLines("DayOne.txt").Select(l => int.Parse(l)).ToList();

            Console.WriteLine($"Input has {lines.Count} lines");
            int prod = lines.SelectMany(a => lines.Select(b => (a + b, a * b)))
                       .Where(sum => sum.Item1 == 2020).First().Item2;

            Console.WriteLine($"Product of lines with sum 2020 is {prod}");
            Console.WriteLine("Day one - End of part One");
        }
Пример #6
0
        public static void PartTwo()
        {
            Console.WriteLine("Day seven - Part Two");
            List <BagRule> rules = PuzzleInputHelper.GetInputLines("DaySeven.txt")
                                   .Select(BagRule.FromLine)
                                   .ToList();

            int sumOfBags = rules.First(br => br.Color == "shiny gold").BagsInside(rules).Sum();

            Console.WriteLine($"A shiny gold bag will contain {sumOfBags} bags in it");
            Console.WriteLine("Day seven - End of Part Two");
        }
Пример #7
0
        public static void PartOne()
        {
            Console.WriteLine("Day Nine - Part One");
            List <BigInteger> inputs = PuzzleInputHelper.GetInputLines("DayNine.txt")
                                       .Select(BigInteger.Parse).ToList();
            var valids = inputs.Select((value, index) =>
            {
                if (index < 25)
                {
                    return(value, true);
                }
                return(value, GetValidSumsFor(inputs.Skip(index - 25).Take(25)).Contains(value));
            });

            Console.WriteLine($"The first number to not respect a valid sum is {valids.First(i => !i.Item2).Item1}");
            Console.WriteLine("Day Nine - End of Part One");
        }
Пример #8
0
        //2037 too high
        public static void PartTwo()
        {
            Console.WriteLine("Day eight - Part Two");
            OpcodeInstruction[] opcodes = PuzzleInputHelper.GetInputLines("DayEight.txt")
                                          .Select(OpcodeInstruction.FromLine).ToArray();
            OpcodeMachine opm = new OpcodeMachine(opcodes);

            try
            {
                opm.BreakLoop();
                Console.WriteLine("Program terminated with accumulator = " + opm.Accumulator);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine("Day eight - End of Part Two");
        }
Пример #9
0
        public static void PartTwo()
        {
            Console.WriteLine("Day Nine - Part Two");
            List <BigInteger> inputs = PuzzleInputHelper.GetInputLines("DayNine.txt")
                                       .Select(BigInteger.Parse).ToList();
            BigInteger answerToPartOne = 373803594;
            BigInteger runningSum      = inputs[0];
            int        minIndex        = 0;
            int        maxIndex        = 0;

            while (runningSum != answerToPartOne)
            {
                if (runningSum > answerToPartOne || runningSum < 0)
                {
                    runningSum -= inputs[minIndex];
                    minIndex++;
                }
                else if (runningSum < answerToPartOne)
                {
                    maxIndex++;
                    runningSum += inputs[maxIndex];
                }
            }
            Console.WriteLine($"Sum of numbers between {minIndex} and {maxIndex} indexes are {answerToPartOne}");
            BigInteger sum = 0;
            BigInteger min = 0;
            BigInteger max = 0;

            for (int i = minIndex; i < maxIndex + 1; i++)
            {
                if (min == 0 || inputs[i] < min)
                {
                    min = inputs[i];
                }
                if (max < inputs[i])
                {
                    max = inputs[i];
                }
                sum += inputs[i];
            }
            Console.WriteLine($"Sum is {sum}");
            Console.WriteLine($"min + max = {min + max}");
            Console.WriteLine("Day Nine - End of Part Two");
        }