Esempio n. 1
0
        public static void PartOne()
        {
            string input = InputHelper.GetInputFromFile("22");

            string[]      lines = input.Split(Environment.NewLine);
            StackShuffler stack = new StackShuffler();

            stack.Start(10007);

            foreach (string line in lines)
            {
                if (line.StartsWith("deal with increment "))
                {
                    stack.DealWithIncrement(int.Parse(line.Split(' ')[3]));
                }
                if (line.StartsWith("cut "))
                {
                    stack.CutNCards(int.Parse(line.Split(' ')[1]));
                }
                if (line == "deal into new stack")
                {
                    stack.DealIntoNewStack();
                }
            }

            Console.WriteLine("Position of card 2019 : " + stack.GetStack().IndexOf(2019));
        }
Esempio n. 2
0
        public static void PartTwo()
        {
            string input = InputHelper.GetInputFromFile("14");

            string[]      lines   = input.Split(Environment.NewLine);
            List <Recipe> recipes = lines.Select(l => new Recipe(l)).ToList();

            Console.WriteLine("Recipes stored.");
            Console.WriteLine("Recipe for FUEL is : " + recipes.First(r => r.Product.Id == "FUEL"));

            long increment    = 1000000;
            long total        = increment;
            long previousUsed = 0;
            long used         = 0;

            while (increment > 0)
            {
                RecipeRobot bot = new RecipeRobot(recipes);
                bot.Make(total, "FUEL");
                used = bot.GetUsed("ORE");
                if (used > 1000000000000)
                {
                    total     -= increment;
                    increment /= 10;
                    used       = previousUsed;
                }
                else
                {
                    total += increment;
                }
                previousUsed = used;
            }

            Console.WriteLine("Produced " + total + " FUEL using " + used + " ORE");
        }
Esempio n. 3
0
        public static void PartOne()
        {
            string input = InputHelper.GetInputFromFile("6");

            IEnumerable <(string Orbitee, string Orbiter)> orbits = input.Split("\r\n").Select(line => { var parts = line.Split(")"); return(parts[0], parts[1]); });

            OrbitTree         tree = new OrbitTree();
            Queue <Orbitable> orbitablesToProcess = new Queue <Orbitable>();

            orbitablesToProcess.Enqueue(tree.CenterOfMass);
            while (orbitablesToProcess.Count() > 0)
            {
                Orbitable currentOrbitable = orbitablesToProcess.Dequeue();
                var       orbiters         = orbits.Where(o => o.Orbitee == currentOrbitable.Code).Select(o => o.Orbiter);
                foreach (string orbiterCode in orbiters)
                {
                    Orbitable orbitable = new Orbitable(orbiterCode, currentOrbitable);
                    currentOrbitable.Orbiters.Add(orbitable);
                    orbitablesToProcess.Enqueue(orbitable);
                }
            }

            //Display(tree.CenterOfMass);
            Console.WriteLine($"Weight : {Weight(tree.CenterOfMass)}");
        }
Esempio n. 4
0
        public static void PartTwo()
        {
            string input         = InputHelper.GetInputFromFile("16");
            int    trailerLength = (input.Length * 10000) - int.Parse(input.Substring(0, 7));
            string longInput     = input.Substring(input.Length - trailerLength % 650);

            for (int i = 0; i < trailerLength / 650; i++)
            {
                longInput += input;
            }
            Console.WriteLine("Long input has length :" + longInput.Length);

            List <int> list = longInput.ToCharArray().Select(c => c - '0').ToList();

            for (int fft = 0; fft < 100; fft++)
            {
                int sum = 0;
                for (int l = list.Count - 1; l >= 0; l--)
                {
                    sum    += list[l];
                    list[l] = sum % 10;
                }
            }
            Console.WriteLine("After 100 FFT: " + String.Join("", list.Take(8)));
        }
Esempio n. 5
0
        public static void PartTwo()
        {
            string      input = InputHelper.GetInputFromFile("12");
            List <Moon> moons = GetMoons(input);

            int step  = 0;
            int zeroX = 0;
            int zeroY = 0;
            int zeroZ = 0;

            while (zeroX == 0 || zeroY == 0 || zeroZ == 0)
            {
                TimeStep(moons);
                step++;
                if (zeroX == 0 && moons.All(m => m.Velocity.X == 0))
                {
                    zeroX = step;
                }
                if (zeroY == 0 && moons.All(m => m.Velocity.Y == 0))
                {
                    zeroY = step;
                }
                if (zeroZ == 0 && moons.All(m => m.Velocity.Z == 0))
                {
                    zeroZ = step;
                }
            }
            Console.WriteLine($"Zero Velocity after {zeroX}, {zeroY} and {zeroZ} steps for X Y and Z");
            long lcm = LCM(zeroX, LCM(zeroY, zeroZ));

            Console.WriteLine($"LCM is {lcm}, twice that is {lcm*2}");
        }
Esempio n. 6
0
        public static void PartOne()
        {
            string input = InputHelper.GetInputFromFile("20");
            Maze   m     = new Maze(input.ToDoubleCharArray(), new DayTwentyMazeFactory());

            m.ResetCells();
            m.SetDistancesFrom(m.Cells.OfType <MazeEntrance>().First());

            Console.WriteLine("Exit is at distance " + m.Cells.OfType <MazeExit>().First().DistanceFromStart);
        }
Esempio n. 7
0
        public static void PartOne()
        {
            const string file  = "18";
            string       input = InputHelper.GetInputFromFile(file);
            Maze         m     = new Maze(input.Split(Environment.NewLine).Select(c => c.ToCharArray()).ToArray(), new DayEighteenMazeFactory());

            Console.WriteLine(m.GetDisplay());

            (int steps, string keysOrder) = GetFastestPathToAllKeys(m, file);
            Console.WriteLine("Shortest paths to all keys in order " + keysOrder + " is " + steps + " steps long");
        }
Esempio n. 8
0
        public static void PartOne()
        {
            string      input = InputHelper.GetInputFromFile("12");
            List <Moon> moons = GetMoons(input);

            for (int i = 0; i < 1000; i++)
            {
                TimeStep(moons);
            }
            Console.WriteLine("Total energy in the system after 1000 time steps :" + moons.Sum(m => m.Energy));
        }
Esempio n. 9
0
        public static void PartTwo()
        {
            string[] input = InputHelper.GetInputFromFile("3").Split("\n");

            List <Tuple <char, int> > firstWireMoves  = input[0].Split(",").Select(m => new Tuple <char, int>(m[0], int.Parse(m.Substring(1)))).ToList();
            List <Tuple <char, int> > secondWireMoves = input[1].Split(",").Select(m => new Tuple <char, int>(m[0], int.Parse(m.Substring(1)))).ToList();
            List <Tuple <int, int> >  pointsWireOne   = GetTraces(firstWireMoves);
            List <Tuple <int, int> >  pointsWireTwo   = GetTraces(secondWireMoves);

            var intersection = pointsWireOne.Intersect(pointsWireTwo).OrderBy(i => pointsWireOne.IndexOf(i) + pointsWireTwo.IndexOf(i)).First();

            Console.WriteLine($"Sum of steps : {pointsWireOne.IndexOf(intersection)} + {pointsWireTwo.IndexOf(intersection)} + 2 = {pointsWireOne.IndexOf(intersection) + pointsWireTwo.IndexOf(intersection) + 2}");
        }
Esempio n. 10
0
        public static void PartTwo()
        {
            string          input     = InputHelper.GetInputFromFile("10");
            List <string>   lines     = input.Split(Environment.NewLine).ToList();
            List <Asteroid> asteroids = new List <Asteroid>();

            for (int y = 0; y < lines.Count; y++)
            {
                for (int x = 0; x < lines[0].Length; x++)
                {
                    if (lines[y][x] == '#')
                    {
                        asteroids.Add(new Asteroid(x, y));
                    }
                }
            }

            Asteroid baseAsteroid = asteroids.First(a => a.X == 30 && a.Y == 34);

            asteroids.Remove(baseAsteroid);
            foreach (Asteroid asteroid in asteroids)
            {
                asteroid.Angle    = ((Angle(asteroid, baseAsteroid) * 180 / Math.PI) + 270) % 360;
                asteroid.Distance = Distance(asteroid, baseAsteroid);
            }
            Console.WriteLine($"Angles go from {asteroids.Min(a => a.Angle)} to {asteroids.Max(a => a.Angle)}");
            IOrderedEnumerable <IGrouping <double, Asteroid> > groups = asteroids.GroupBy(a => a.Angle * 1000).OrderBy(a => a.Key);
            var targetAngles = groups.ToDictionary(t => t.Key, t => t.Select(a => a).OrderBy(a => a.Distance).ToList());
            int d            = 0;
            int g            = 0;

            while (d < 200)
            {
                Asteroid destroyed = targetAngles.ElementAt(g).Value.First();
                destroyed.Destroyed = true;
                Console.WriteLine($"{d+1}th destroyed from group {g} : {destroyed.X} {destroyed.Y} angle {destroyed.Angle}");
                targetAngles.ElementAt(g).Value.Remove(destroyed);
                if (targetAngles.ElementAt(g).Value.Count == 0)
                {
                    targetAngles.Remove(targetAngles.ElementAt(g).Key);
                }
                else
                {
                    g = g + 1;
                }
                g = g % targetAngles.Count;
                Display(asteroids, baseAsteroid);
                Console.ReadLine();
                d++;
            }
        }
Esempio n. 11
0
        public static void PartOne()
        {
            string     input   = InputHelper.GetInputFromFile("16");
            List <int> phase   = input.ToCharArray().Select(c => c - '0').ToList();
            List <int> pattern = new List <int> {
                0, 1, 0, -1
            };

            for (int p = 0; p < 100; p++)
            {
                phase = NextPhase(phase, pattern);
            }
            Console.WriteLine("Final phase is " + String.Join(' ', phase.Take(8)));
        }
Esempio n. 12
0
            public static void PartOne()
            {
                string inputLines = InputHelper.GetInputFromFile("1");

                int sum = 0;

                foreach (string line in inputLines.Split(Environment.NewLine))
                {
                    int value = (int)Math.Floor(int.Parse(line) / 3.0) - 2;
                    sum += value;
                }

                Console.WriteLine(sum);
            }
Esempio n. 13
0
        public static void PartOne()
        {
            string input = InputHelper.GetInputFromFile("14");

            string[]      lines   = input.Split(Environment.NewLine);
            List <Recipe> recipes = lines.Select(l => new Recipe(l)).ToList();

            Console.WriteLine("Recipes stored.");
            Console.WriteLine("Recipe for FUEL is : " + recipes.First(r => r.Product.Id == "FUEL"));

            RecipeRobot bot = new RecipeRobot(recipes);

            bot.Make(1, "FUEL");
            Console.WriteLine("Used : " + bot.UsedElements.Count(e => e.Id == "ORE") + " ORE");
        }
Esempio n. 14
0
        public static void PartTwo()
        {
            string input = InputHelper.GetInputFromFile("15_maze");

            Maze maze = new Maze(input.Split(Environment.NewLine).Select(l => l.ToArray()).ToArray(), new DayFifteenMazeFactory());

            int minutes = 0;

            while (maze.Cells.Any(c => c is MazeCorridor))
            {
                maze.PropagateOxygen();
                minutes++;
            }
            Console.WriteLine("Finished propagation of oxygen after " + minutes + " minutes");
        }
Esempio n. 15
0
        public static void PartTwo()
        {
            string       input  = InputHelper.GetInputFromFile("8");
            int          width  = 25;
            int          height = 6;
            List <Layer> layers = new List <Layer>();
            int          i      = 0;

            while (i + (width * height) - 1 < input.Length)
            {
                string layerString = input.Substring(i, width * height);
                Layer  l           = new Layer();
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        l.Pixels[x, y] = layerString[x + (y * width)] - '0';
                    }
                }
                layers.Add(l);
                i += (width * height);
            }

            Bitmap bmp = new Bitmap(width, height);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    foreach (Layer l in layers)
                    {
                        bmp.SetPixel(x, y, Color.Transparent);
                        if (l.Pixels[x, y] == 2)
                        {
                            continue;
                        }
                        Color color = l.Pixels[x, y] == 0 ? Color.Black : Color.White;
                        bmp.SetPixel(x, y, color);
                        break;
                    }
                }
            }
            bmp.Save(@"C:\Temp\DayEight.bmp");
        }
Esempio n. 16
0
        public static void PartOne()
        {
            string lines = InputHelper.GetInputFromFile("17_maze");

            char[][] maze = lines.Split("\n").Select(l => l.ToCharArray()).ToArray();
            int      sum  = 0;

            for (int y = 0; y < maze.Length; y++)
            {
                for (int x = 0; x < maze[y].Length; x++)
                {
                    if (maze[y][x] == 'O')
                    {
                        sum += x * y;
                    }
                }
            }
            Console.WriteLine(sum);
        }
Esempio n. 17
0
            public static void PartTwo()
            {
                string inputLines = InputHelper.GetInputFromFile("1");

                int sum = 0;

                foreach (string line in inputLines.Split(Environment.NewLine))
                {
                    int value       = (int)Math.Floor(int.Parse(line) / 3.0) - 2;
                    int additionnal = (int)Math.Floor(value / 3.0) - 2;
                    while (additionnal > 0)
                    {
                        value      += additionnal;
                        additionnal = (int)Math.Floor(additionnal / 3.0) - 2;
                    }
                    sum += value;
                }

                Console.WriteLine(sum);
            }
Esempio n. 18
0
        public static void PartOne()
        {
            string[] input = InputHelper.GetInputFromFile("3").Split("\n");

            List <Tuple <char, int> > firstWireMoves  = input[0].Split(",").Select(m => new Tuple <char, int>(m[0], int.Parse(m.Substring(1)))).ToList();
            List <Tuple <char, int> > secondWireMoves = input[1].Split(",").Select(m => new Tuple <char, int>(m[0], int.Parse(m.Substring(1)))).ToList();
            Queue <Tuple <int, int> > pointsWireOne   = GetPoints(firstWireMoves);
            Queue <Tuple <int, int> > pointsWireTwo   = GetPoints(secondWireMoves);

            (int xmin, int xmax) = (Math.Min(pointsWireOne.Min(p => p.Item1), pointsWireTwo.Min(p => p.Item1)), Math.Max(pointsWireOne.Max(p => p.Item1), pointsWireTwo.Max(p => p.Item1)));
            (int ymin, int ymax) = (Math.Min(pointsWireOne.Min(p => p.Item2), pointsWireTwo.Min(p => p.Item2)), Math.Max(pointsWireOne.Max(p => p.Item2), pointsWireTwo.Max(p => p.Item2)));
            int width  = 3 + xmax - xmin;
            int height = 3 + ymax - ymin;

            char[,] grid = new char[height, width];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    grid[y, x] = ' ';
                }
            }
            Queue <Tuple <int, int> > intersections = new Queue <Tuple <int, int> >();

            Trace(pointsWireOne, 'a', xmin, ymin, grid, intersections);
            Trace(pointsWireTwo, 'b', xmin, ymin, grid, intersections);
            Display(grid, width, height);
            int minDistance = int.MaxValue;

            while (intersections.Count > 0)
            {
                var t        = intersections.Dequeue();
                int distance = Math.Abs(t.Item1 - 1 + xmin) + Math.Abs(t.Item2 - 1 + ymin);
                if (distance < minDistance)
                {
                    minDistance = distance;
                }
            }
            Console.WriteLine("Min distance : " + minDistance);
        }
Esempio n. 19
0
        public static void PartTwo()
        {
            string input = InputHelper.GetInputFromFile("6");

            IEnumerable <(string Orbitee, string Orbiter)> orbits = input.Split("\r\n").Select(line => { var parts = line.Split(")"); return(parts[0], parts[1]); });

            OrbitTree         tree = new OrbitTree();
            Queue <Orbitable> orbitablesToProcess = new Queue <Orbitable>();

            orbitablesToProcess.Enqueue(tree.CenterOfMass);
            Orbitable me = null, santa = null;

            while (orbitablesToProcess.Count() > 0)
            {
                Orbitable currentOrbitable = orbitablesToProcess.Dequeue();
                var       orbiters         = orbits.Where(o => o.Orbitee == currentOrbitable.Code).Select(o => o.Orbiter);
                foreach (string orbiterCode in orbiters)
                {
                    Orbitable orbitable = new Orbitable(orbiterCode, currentOrbitable);
                    if (orbiterCode == "YOU")
                    {
                        me = orbitable;
                    }
                    if (orbiterCode == "SAN")
                    {
                        santa = orbitable;
                    }
                    currentOrbitable.Orbiters.Add(orbitable);
                    orbitablesToProcess.Enqueue(orbitable);
                }
            }

            //Display(tree.CenterOfMass);
            int distanceBetween = GetDistanceBetween(tree, me, santa);

            Console.WriteLine($"Distance: {distanceBetween}");
        }
Esempio n. 20
0
        public static void PartOne()
        {
            string       input  = InputHelper.GetInputFromFile("8");
            List <Layer> layers = new List <Layer>();
            int          i      = 0;

            while (i + 25 * 6 < input.Length)
            {
                string layerString = input.Substring(i, 25 * 6);
                Layer  l           = new Layer();
                for (int x = 0; x < 25; x++)
                {
                    for (int y = 0; y < 6; y++)
                    {
                        switch (l.Pixels[x, y])
                        {
                        case 0:
                            l.Zeroes++;
                            break;

                        case 1:
                            l.Ones++;
                            break;

                        case 2:
                            l.Twos++;
                            break;
                        }
                    }
                }
                layers.Add(l);
                i += (25 * 6);
            }
            Layer fattest = layers.First(l => l.Zeroes == layers.Min(l => l.Zeroes));

            Console.WriteLine("Result: " + fattest.Ones * fattest.Twos);
        }