Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var inputList      = (args.Count() > 0) ? InputLoader.GetInputAsList(args[0]) : InputLoader.GetInputAsList("input.txt");
            var passportsInput = new List <List <string> >();
            var i = 0;

            passportsInput.Add(new List <string>());
            foreach (var input in inputList)
            {
                if (!string.IsNullOrWhiteSpace(input))
                {
                    passportsInput[i].Add(input);
                }
                else
                {
                    i++;
                    passportsInput.Add(new List <string>());
                }
            }
            var passportsText = new List <string>();

            foreach (var list in passportsInput)
            {
                var passportText = "";
                list.ForEach(delegate(string e) { passportText += " " + e; });
                passportsText.Add(passportText.Trim());
            }
            var passports = passportsText.Select(anon => Passport.ParseString(anon)).ToList();

            Console.WriteLine($"Number of passports with all required fields (part 1): {passports.Where(anon => anon.HasRequiredFields).Count()}");
            Console.WriteLine($"Number of passports with correct fields (part 2): {passports.Where(anon => anon.HasRequiredFields && anon.AreFieldsValid).Count()}");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var inputList    = (args.Count() > 0) ? InputLoader.GetInputAsList(args[0]) : InputLoader.GetInputAsList("input.txt");
            var customsInput = new List <List <string> >();
            var i            = 0;

            customsInput.Add(new List <string>());
            foreach (var input in inputList)
            {
                if (!string.IsNullOrWhiteSpace(input))
                {
                    customsInput[i].Add(input);
                }
                else
                {
                    i++;
                    customsInput.Add(new List <string>());
                }
            }
            var customsText = new List <string>();

            foreach (var list in customsInput)
            {
                var passportText = "";
                list.ForEach(delegate(string e) { passportText += e; });
                customsText.Add(passportText);
            }
            // part 1
            var numberOfTotalAnswers = customsText
                                       .Select(anon => anon.Distinct())
                                       .Sum(anon => anon.Count());

            Console.WriteLine($"Number for part 1: {numberOfTotalAnswers}");
            // part 2 - very dirty way of doing it
            var customsMemberText      = new List <List <string> >();
            var customsResultIntersect = new List <int>();

            foreach (var list in customsInput)
            {
                var passportText = "";
                list.ForEach(delegate(string e) { passportText += " " + e; });
                customsMemberText.Add(passportText.Trim().Split(' ').ToList());
            }
            for (i = 0; i < customsMemberText.Count; i++)
            {
                var first = customsMemberText[i][0];
                for (int j = 1; j < customsMemberText[i].Count; j++)
                {
                    first = new string(first.Intersect(customsMemberText[i][j]).ToArray());
                }
                customsResultIntersect.Add(first.Length);
            }
            var part2Result = customsResultIntersect.Sum();

            Console.WriteLine($"Number for part 2: {part2Result}");
            Console.ReadLine();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var input    = InputLoader.GetInputAsList("input.txt");
            var adapters = input
                           .Select(x => int.Parse(x))
                           .ToList();

            adapters.Add(adapters.Max() + 3);
            orderedAdapters = adapters.OrderBy(x => x).ToList();
            int jolt1 = 0, jolt2 = 0, jolt3 = 0, lastAdapter = 0;

            foreach (var adapter in orderedAdapters)
            {
                switch (adapter - lastAdapter)
                {
                case 3:
                    jolt3++;
                    break;

                case 2:
                    jolt2++;
                    break;

                default:
                    jolt1++;
                    break;
                }
                lastAdapter = adapter;
            }
            Console.WriteLine($"Solution for task 1: {jolt1*jolt3}");
            // solution 2
            allMustHops = new List <SortedSet <int> >();
            lastAdapter = 0;
            foreach (var adapter in orderedAdapters)
            {
                if ((adapter - lastAdapter) == 3)
                {
                    allMustHops.Add(new SortedSet <int>()
                    {
                        lastAdapter, adapter
                    });
                }
                lastAdapter = adapter;
            }

            allMustHops = allMustHops
                          .OrderByDescending(x => x.Count)
                          .ThenBy(x => x.First())
                          .ToList();

            orderedAdapters = adapters.OrderBy(x => x).ToList();

            Console.WriteLine($"Solution for task 2: {CalculateHops(0)}");
            Console.ReadLine();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var input = InputLoader.GetInputAsList("input.txt")
                        .Select(x => long.Parse(x))
                        .ToList();
            var inputDict = new Dictionary <int, long>();

            for (int i = 0; i < input.Count; i++)
            {
                inputDict.Add(i, input[i]);
            }

            for (int i = 25; i < inputDict.Keys.Count; i++)
            {
                var preambule = inputDict
                                .Where(x => x.Key < i && x.Key >= i - 25)
                                .Select(x => x.Value)
                                .ToList();
                if (!IsSumOfPreambule(inputDict[i], preambule))
                {
                    break;
                }
            }
            Console.WriteLine($"Solution for task 1: {NumberState}");

            var numbersSum = new List <long>();

            for (int i = 0; i < inputDict.Keys.Count; i++)
            {
                var testSum    = inputDict[i];
                var endingBool = false;
                numbersSum.Add(inputDict[i]);
                foreach (var subNumber in inputDict.Values.Skip(i + 1))
                {
                    testSum += subNumber;
                    numbersSum.Add(subNumber);
                    if (testSum.Equals(NumberState))
                    {
                        endingBool = true;
                        break;
                    }
                }
                if (endingBool)
                {
                    break;
                }
                else
                {
                    numbersSum.Clear();
                }
            }
            Console.WriteLine($"Solution for task 2: {numbersSum.OrderBy(x => x).First()+numbersSum.OrderByDescending(x => x).First()}");
            Console.ReadLine();
        }
Exemplo n.º 5
0
        static void Main()
        {
            var input       = InputLoader.GetInputAsList("input.txt");
            var timestamp   = int.Parse(input[0]);
            var earliestBus = input[1]
                              .Split(',')
                              .Where(x => x != "x")
                              .Select(x => long.Parse(x))
                              .Select(x => new { BusID = x, TimestampRemaining = x - timestamp % x })
                              .OrderBy(x => x.TimestampRemaining)
                              .First();

            Console.WriteLine($"Solution for task 1: {earliestBus.BusID * earliestBus.TimestampRemaining}");

            var inputBusses = input[1]
                              .Split(',')
                              .Select(x => x == "x" ? 0 : long.Parse(x))
                              .ToList();

            var busses = new List <BusHelp>();
            var offset = 0L;

            foreach (var bus in inputBusses)
            {
                if (!bus.Equals(0))
                {
                    busses.Add(new BusHelp()
                    {
                        BusID = bus, OffsetReq = offset
                    });
                }
                offset++;
            }

            var solution2 = 0L;
            var range     = busses.Select(x => x.BusID).Aggregate((x, y) => x * y);

            foreach (var bus in busses)
            {
                var modulo  = (((bus.BusID - bus.OffsetReq) % bus.BusID) + bus.BusID) % bus.BusID;
                var sub     = range / bus.BusID;
                var inverse = GetInverseNumber(sub, bus.BusID);

                solution2 += modulo * sub * inverse;
            }
            solution2 %= range;

            Console.WriteLine($"Solution for task 2: {solution2}");
            Console.ReadLine();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var input = InputLoader.GetInputAsList("input.txt");
            var list  = input
                        .Select(x => new Instruction(x))
                        .ToList();

            var simulation = new Simulation(list);

            simulation.Begin();

            Console.WriteLine($"Solution for task 1: {Math.Abs(simulation.ShipBasic.Location.X) + Math.Abs(simulation.ShipBasic.Location.Y)}");
            Console.WriteLine($"Solution for task 2: {Math.Abs(simulation.ShipWaypoint.Location.X) + Math.Abs(simulation.ShipWaypoint.Location.Y)}");

            Console.ReadLine();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            List <string> inputList = (args.Count() > 0) ? InputLoader.GetInputAsList(args[0]) : InputLoader.GetInputAsList("input.txt");
            var           passwordPart1ValidCount = inputList
                                                    .Select(inputLine => PasswordMinMax.ParseInput(inputLine))
                                                    .Where(password => password?.IsValid ?? false)
                                                    .Count();

            Console.WriteLine($"Number of valid passwords for part 1: {passwordPart1ValidCount}");
            var passwordPart2ValidCount = inputList
                                          .Select(inputLine => PasswordPositions.ParseInput(inputLine))
                                          .Where(password => password?.IsValid ?? false)
                                          .Count();

            Console.WriteLine($"Number of valid passwords for part 2: {passwordPart2ValidCount}");
            Console.ReadLine();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            List <string> inputList = (args.Count() > 0) ? InputLoader.GetInputAsList(args[0]) : InputLoader.GetInputAsList("input.txt");
            var           slope     = new Slope(inputList)
            {
                PositionX = 0, PositionY = 0, MovementRight = 3, MovementBottom = 1
            };

            slope.Start();
            Console.WriteLine($"Trees encountered in part 1: {slope.TreesEncountered}");
            // right 3, below 1
            var slope31 = slope.TreesEncountered;

            // right 1, below 1
            slope.PositionX     = slope.PositionY = 0;
            slope.MovementRight = slope.MovementBottom = 1;
            slope.Start();
            var slope11 = slope.TreesEncountered;

            // right 5, below 1
            slope.PositionX      = slope.PositionY = 0;
            slope.MovementRight  = 5;
            slope.MovementBottom = 1;
            slope.Start();
            var slope51 = slope.TreesEncountered;

            // right 7, below 1
            slope.PositionX      = slope.PositionY = 0;
            slope.MovementRight  = 7;
            slope.MovementBottom = 1;
            slope.Start();
            var slope71 = slope.TreesEncountered;

            // right 1, below 2
            slope.PositionX      = slope.PositionY = 0;
            slope.MovementRight  = 1;
            slope.MovementBottom = 2;
            slope.Start();
            var slope12 = slope.TreesEncountered;

            // part 2
            Console.WriteLine($"Number for part 2: {slope11*slope31*slope51*slope71*slope12}");
            Console.ReadLine();
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var input = InputLoader.GetInputAsList("input.txt");
            var dict  = new Dictionary <int, Instruction>();

            for (int i = 0; i < input.Count; i++)
            {
                dict.Add(i, Instruction.FromInput(input[i]));
            }
            var simulator1 = new BootSimulator(dict);

            simulator1.BeginBoot();
            Console.WriteLine($"Solution for task 1: {simulator1.AccumulatorState}");
            var accResult = 0;

            foreach (var instructionId in dict.Keys)
            {
                var dictCopy = CopyDictionary(dict);
                if (dictCopy[instructionId].Type.Equals(InstructionType.DoNothing))
                {
                    dictCopy[instructionId].Type = InstructionType.Jump;
                    var simulator = new BootSimulator(dictCopy);
                    simulator.BeginBoot();
                    if (simulator.BootSimulatorState.Equals(BootSimulatorState.FinishedSuccess))
                    {
                        accResult = simulator.AccumulatorState;
                        break;
                    }
                }
                else if (dictCopy[instructionId].Type.Equals(InstructionType.Jump))
                {
                    dictCopy[instructionId].Type = InstructionType.DoNothing;
                    var simulator = new BootSimulator(dictCopy);
                    simulator.BeginBoot();
                    if (simulator.BootSimulatorState.Equals(BootSimulatorState.FinishedSuccess))
                    {
                        accResult = simulator.AccumulatorState;
                        break;
                    }
                }
            }
            Console.WriteLine($"Solution for task 2: {accResult}");
            Console.ReadLine();
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            var input = InputLoader.GetInputAsList("input.txt");

            BagDict = input
                      .Select(x => new Bag(x))
                      .ToDictionary(x => x.Name, x => x);
            var prepareParentsList = BagDict
                                     .Where(x => x.Value.ContainsBags.ContainsKey("shiny gold bags"))
                                     .Select(x => x.Key)
                                     .ToList();

            SearchedParents.AddRangeUnique(prepareParentsList);
            RecursionParents(prepareParentsList);
            Console.WriteLine($"Solution for task 1: {SearchedParents.Count}");
            RecursionChildren("shiny gold bags", 1);
            Console.WriteLine($"Solution for task 2: {RollingCount-1}");
            Console.ReadLine();
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            var inputList      = (args.Count() > 0) ? InputLoader.GetInputAsList(args[0]) : InputLoader.GetInputAsList("input.txt");
            var boardingPasses = inputList.Select(anon => BoardingPass.ParseInput(anon)).ToList();
            var highestID      = boardingPasses.Max(anon => anon.SeatID);

            Console.WriteLine($"The highest seat ID (part 1): {highestID}");
            var emptySeats = new List <BoardingPass>();

            for (var i = 0; i < 128; i++)
            {
                for (var j = 0; j < 8; j++)
                {
                    var doesExistInPasses = boardingPasses
                                            .Where(anon => anon.SeatID.Equals(i * 8 + j))
                                            .Count();
                    if (doesExistInPasses == 0)
                    {
                        emptySeats.Add(new BoardingPass()
                        {
                            Row = i, Column = j
                        });
                    }
                }
            }
            foreach (var mySeat in emptySeats)
            {
                var leftSeatExists = boardingPasses
                                     .Where(anon => (mySeat.SeatID - 1).Equals(anon.SeatID))
                                     .Count() != 0;
                var rightSeatExists = boardingPasses
                                      .Where(anon => (mySeat.SeatID + 1).Equals(anon.SeatID))
                                      .Count() != 0;
                if (leftSeatExists && rightSeatExists)
                {
                    Console.WriteLine($"My seat is ID: {mySeat.SeatID}");
                }
            }
            Console.ReadLine();
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            var input = InputLoader.GetInputAsList("input.txt");
            var list  = input
                        .Select(x => new List <Place>(x.ToList().Select(y => new Place(y)).ToList()))
                        .ToList();

            var simulation1 = new SeatingSimulation(list);

            simulation1.BeginTask1Simulation();

            var simulationSeats = simulation1.Places.Select(x => x.Where(y => y.Type.Equals(PlaceType.TakenSeat)).Count()).Sum();

            Console.WriteLine($"Solution for task 1: {simulationSeats}");

            var simulation2 = new SeatingSimulation(list);

            simulation2.BeginTask2Simulation();

            simulationSeats = simulation2.Places.Select(x => x.Where(y => y.Type.Equals(PlaceType.TakenSeat)).Count()).Sum();
            Console.WriteLine($"Solution for task 2: {simulationSeats}");

            Console.ReadLine();
        }