Beispiel #1
0
        public void Task1()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day6.txt");
            //AdventUtils.WriteLines(lines);

            IDictionary <string, IList <string> > orbits      = new Dictionary <string, IList <string> >();
            IDictionary <string, int>             orbitsCount = new Dictionary <string, int>();

            foreach (string line in lines)
            {
                string inOrbit  = line.Substring(0, line.IndexOf(')'));
                string outOrbit = line.Substring(line.IndexOf(')') + 1, line.Length - 1 - inOrbit.Length);
                if (!orbits.ContainsKey(outOrbit))
                {
                    orbits[outOrbit] = new List <string>();
                }

                orbits[outOrbit].Add(inOrbit);
            }

            int count = 0;

            foreach (string orbit in orbits.Keys)
            {
                count += CountOrbits(orbit, orbits, orbitsCount);
            }

            //621125
            Console.WriteLine("Day 6 task 1 : " + count);
        }
Beispiel #2
0
        private ISet <Point> ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day10.txt");
            //AdventUtils.WriteLines(lines);

            ISet <Point> asteroids = new HashSet <Point>();
            int          x = 0; int y = 0;

            foreach (string line in lines)
            {
                char[] input = line.ToCharArray();
                for (int index = 0; index < input.Length; index++)
                {
                    if (input[index] == '#')
                    {
                        asteroids.Add(new Point()
                        {
                            X = x, Y = y
                        });
                    }

                    x++;
                }

                x = 0;  y++;
            }


            return(asteroids);
        }
Beispiel #3
0
        public void Task2()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day6.txt");
            //AdventUtils.WriteLines(lines);

            IDictionary <string, IList <string> > orbits      = new Dictionary <string, IList <string> >();
            IDictionary <string, int>             orbitsCount = new Dictionary <string, int>();

            foreach (string line in lines)
            {
                string inOrbit  = line.Substring(0, line.IndexOf(')'));
                string outOrbit = line.Substring(line.IndexOf(')') + 1, line.Length - 1 - inOrbit.Length);
                if (!orbits.ContainsKey(outOrbit))
                {
                    orbits[outOrbit] = new List <string>();
                }

                orbits[outOrbit].Add(inOrbit);

                if (!orbits.ContainsKey(inOrbit))
                {
                    orbits[inOrbit] = new List <string>();
                }

                orbits[inOrbit].Add(outOrbit);
            }

            orbitsCount["YOU"] = 0;
            TravelOrbits("YOU", "SAN", orbits, orbitsCount);
            int count = orbitsCount["SAN"] - 2;

            //550
            Console.WriteLine("Day 6 task 2 : " + count);
        }
Beispiel #4
0
        public void Task2()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day8.txt");
            //AdventUtils.WriteLines(lines);

            IList <char[][]> layers = new List <char[][]>();

            char[] input = lines[0].ToCharArray();

            for (int index = 0; index < input.Length / 150; index++)
            {
                layers.Add(new char[6][]);

                for (int layerIndex = 0; layerIndex < 6; layerIndex++)
                {
                    layers[index][layerIndex] = new char[25];
                    for (int layerIndex2 = 0; layerIndex2 < 25; layerIndex2++)
                    {
                        int finaIndex = index * 150 + (layerIndex * 25 + layerIndex2);
                        layers[index][layerIndex][layerIndex2] = input[finaIndex];
                    }
                }
            }

            Console.WriteLine("Day 8 task 2 : ");  //ZPZUB
            for (int layerIndex = 0; layerIndex < 6; layerIndex++)
            {
                for (int layerIndex2 = 0; layerIndex2 < 25; layerIndex2++)
                {
                    bool writePixes = false;
                    foreach (char[][] layer in layers)
                    {
                        if (layer[layerIndex][layerIndex2] == '1')
                        {
                            Console.Write("#");
                            writePixes = true;
                            break;
                        }
                        else if (layer[layerIndex][layerIndex2] == '0')
                        {
                            Console.Write(" ");
                            writePixes = true;
                            break;
                        }
                    }
                    if (!writePixes)
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
        }
Beispiel #5
0
        private int[] ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day4.txt");

            //AdventUtils.WriteLines(lines);
            int[] output = new int[2];

            output[0] = int.Parse(lines[0].Split('-')[0]);
            output[1] = int.Parse(lines[0].Split('-')[1]);

            return(output);
        }
Beispiel #6
0
        private string[][] ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day3.txt");

            //AdventUtils.WriteLines(lines);
            string[][] output = new string[2][];

            output[0] = lines[0].Split(',');
            output[1] = lines[1].Split(',');

            return(output);
        }
Beispiel #7
0
        public void Task2()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day12.txt");
            //AdventUtils.WriteLines(lines);
            IList <Moon> Moons = ReadAndParse();

            long result = 0;  long resultX = 1; long resultY = 1; long resultZ = 1;

            IList <MoonSingleAxis> MoonsSingleAxis = new List <MoonSingleAxis>();
            IList <MoonSingleAxis> MoonsSingleAxisInitial = new List <MoonSingleAxis>();

            foreach (Moon moon in Moons)
            {
                MoonsSingleAxis.Add(new MoonSingleAxis {
                    Pos = moon.PosX, Vel = moon.VelX
                });
                MoonsSingleAxisInitial.Add(new MoonSingleAxis {
                    Pos = moon.PosX, Vel = moon.VelX
                });
            }
            resultX *= SinglAxisMovement(MoonsSingleAxis, MoonsSingleAxisInitial);
            MoonsSingleAxis.Clear(); MoonsSingleAxisInitial.Clear();

            foreach (Moon moon in Moons)
            {
                MoonsSingleAxis.Add(new MoonSingleAxis {
                    Pos = moon.PosY, Vel = moon.VelY
                });
                MoonsSingleAxisInitial.Add(new MoonSingleAxis {
                    Pos = moon.PosY, Vel = moon.VelY
                });
            }
            resultY *= SinglAxisMovement(MoonsSingleAxis, MoonsSingleAxisInitial);
            MoonsSingleAxis.Clear(); MoonsSingleAxisInitial.Clear();
            result = AdventUtils.LCM(resultX, resultY);

            foreach (Moon moon in Moons)
            {
                MoonsSingleAxis.Add(new MoonSingleAxis {
                    Pos = moon.PosZ, Vel = moon.VelZ
                });
                MoonsSingleAxisInitial.Add(new MoonSingleAxis {
                    Pos = moon.PosZ, Vel = moon.VelZ
                });
            }
            resultZ *= SinglAxisMovement(MoonsSingleAxis, MoonsSingleAxisInitial);
            result   = AdventUtils.LCM(result, resultZ);

            //518311327635164
            Console.WriteLine("Day 12 task 1 : " + result);
        }
Beispiel #8
0
        private long[] ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day9.txt");

            //AdventUtils.WriteLines(lines);

            string[] input    = lines[0].Split(',');
            long[]   inputInt = new long[input.Length];
            for (int index = 0; index < input.Length; index++)
            {
                inputInt[index] = long.Parse(input[index]);
            }

            return(inputInt);
        }
Beispiel #9
0
        public void Task2()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day1.txt");
            //AdventUtils.WriteLines(lines);

            int result = 0;

            foreach (string line in lines)
            {
                result += AddFuelForFuel(int.Parse(line));
            }

            //5093620
            Console.WriteLine("Day 1 task 2 : " + result);
        }
Beispiel #10
0
        private long[] ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day16.txt");

            //AdventUtils.WriteLines(lines);

            char[] input    = lines[0].ToCharArray();
            long[] inputInt = new long[input.Length];
            for (int index = 0; index < input.Length; index++)
            {
                inputInt[index] = long.Parse(input[index].ToString());
            }

            return(inputInt);
        }
Beispiel #11
0
        public void Task1()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day1.txt");
            //AdventUtils.WriteLines(lines);

            int result = 0;

            foreach (string line in lines)
            {
                result += (int)Math.Floor((decimal)int.Parse(line) / 3);
                result -= 2;
            }


            //3397667
            Console.WriteLine("Day 1 task 1 : " + result);
        }
Beispiel #12
0
        private IList <Moon> ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day12.txt");
            IList <Moon>   Moons = new List <Moon>();

            foreach (string line in lines)
            {
                string[] cordinates = line.Split(',');
                Moon     moon       = new Moon();
                moon.PosX = long.Parse(cordinates[0].Substring(cordinates[0].IndexOf('=') + 1));
                moon.PosY = long.Parse(cordinates[1].Substring(cordinates[1].IndexOf('=') + 1));
                moon.PosZ = long.Parse(cordinates[2].Substring(cordinates[2].IndexOf('=') + 1, cordinates[2].Length - 4));

                Moons.Add(moon);
            }

            return(Moons);
        }
Beispiel #13
0
        private IDictionary <Material, IList <Material> > ReadAndParse()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day14.txt");
            //AdventUtils.WriteLines(lines);

            IDictionary <Material, IList <Material> > materials = new Dictionary <Material, IList <Material> >();

            foreach (string line in lines)
            {
                string[] input = line.Split(new string[2] {
                    ",", "=>"
                }, StringSplitOptions.None);
                string[] endMaterial = input[input.Length - 1].Trim().Split(new string[1] {
                    " "
                }, StringSplitOptions.None);
                int      endMaterialQuantity = int.Parse(endMaterial[0]);
                string   endMaterialName     = endMaterial[1];
                Material material            = new Material()
                {
                    Name = endMaterialName, Quantity = endMaterialQuantity
                };
                if (!materials.ContainsKey(material))
                {
                    materials[material] = new List <Material> ();
                }

                string[] partMaterial = line.Substring(0, line.IndexOf(" =>")).Split(new string[1] {
                    ","
                }, StringSplitOptions.None);
                for (int index = 0; index < partMaterial.Length; index = index + 1)
                {
                    int    quantity     = int.Parse(partMaterial[index].Trim().Split(' ')[0].Trim());
                    string materialName = partMaterial[index].Trim().Split(' ')[1].Trim();
                    materials[material].Add(new Material()
                    {
                        Name = materialName, Quantity = quantity
                    });
                }
            }

            return(materials);
        }
Beispiel #14
0
        public void Task1()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day8.txt");
            //AdventUtils.WriteLines(lines);
            IList <IDictionary <int, int> > layers = new List <IDictionary <int, int> >();

            char[] input = lines[0].ToCharArray();

            for (int index = 0; index < input.Length / 150; index++)
            {
                IDictionary <int, int> layer = new Dictionary <int, int> {
                    [0] = 0, [1] = 0, [2] = 0
                };
                layers.Add(layer);

                for (int layerIndex = 0; layerIndex < 150; layerIndex++)
                {
                    int finaIndex  = index * 150 + layerIndex;
                    int finalInput = int.Parse(input[finaIndex].ToString());
                    layer[finalInput]++;
                }
            }

            int min = 151; int max = 0;

            foreach (IDictionary <int, int> layer in layers)
            {
                if (min > layer[0])
                {
                    min = layer[0];
                    max = layer[1] * layer[2];
                }
            }
            //1690
            Console.WriteLine("Day 8 task 1 : " + max);
        }
Beispiel #15
0
        public void Task1()
        {
            IList <string> lines = AdventUtils.ReadFileByLines(@"..\..\..\Files\Day12.txt");
            //AdventUtils.WriteLines(lines);
            IList <Moon> Moons = ReadAndParse();

            long result = 0;

            for (int index = 0; index < 1000; index++)
            {
                for (int outMoon = 0; outMoon < Moons.Count; outMoon++)
                {
                    for (int inMoon = outMoon + 1; inMoon < Moons.Count; inMoon++)
                    {
                        if (Moons[outMoon].PosX < Moons[inMoon].PosX)
                        {
                            Moons[outMoon].VelX++; Moons[inMoon].VelX--;
                        }
                        else if (Moons[outMoon].PosX > Moons[inMoon].PosX)
                        {
                            Moons[outMoon].VelX--; Moons[inMoon].VelX++;
                        }

                        if (Moons[outMoon].PosY < Moons[inMoon].PosY)
                        {
                            Moons[outMoon].VelY++; Moons[inMoon].VelY--;
                        }
                        else if (Moons[outMoon].PosY > Moons[inMoon].PosY)
                        {
                            Moons[outMoon].VelY--; Moons[inMoon].VelY++;
                        }

                        if (Moons[outMoon].PosZ < Moons[inMoon].PosZ)
                        {
                            Moons[outMoon].VelZ++; Moons[inMoon].VelZ--;
                        }
                        else if (Moons[outMoon].PosZ > Moons[inMoon].PosZ)
                        {
                            Moons[outMoon].VelZ--; Moons[inMoon].VelZ++;
                        }
                    }
                }

                for (int outMoon = 0; outMoon < Moons.Count; outMoon++)
                {
                    Moons[outMoon].PosX += Moons[outMoon].VelX;
                    Moons[outMoon].PosY += Moons[outMoon].VelY;
                    Moons[outMoon].PosZ += Moons[outMoon].VelZ;
                }
            }

            for (int outMoon = 0; outMoon < Moons.Count; outMoon++)
            {
                long potencialEnergy = Math.Abs(Moons[outMoon].PosX) + Math.Abs(Moons[outMoon].PosY) + Math.Abs(Moons[outMoon].PosZ);
                long kineticEnergy   = Math.Abs(Moons[outMoon].VelX) + Math.Abs(Moons[outMoon].VelY) + Math.Abs(Moons[outMoon].VelZ);
                result += potencialEnergy * kineticEnergy;
            }

            //7928
            Console.WriteLine("Day 12 task 1 : " + result);
        }