Example #1
0
        public override async Task Part1()
        {
            var removegtstSigns = PuzzleInput.Replace("<", "").Replace(">", "");
            var lines           = removegtstSigns.Split("\r\n").Select(x => x.Split(',').Select(y => new string(y.Trim().SkipWhile(z => z == '=').ToArray()))).Select(x => x.Select(y => y.Substring(y.IndexOf('=') + 1).Trim()));;
            var moonsLijst      = lines.Select(x => new Moon(Convert.ToInt32(x.First()), Convert.ToInt32(x.Skip(1).First()), Convert.ToInt32(x.Last())));
            var moons           = moonsLijst.ToList();


            for (int count = 0; count < 1000; count++)
            {
                for (int i = 0; i < moons.Count(); i++)
                {
                    var moon     = moons[i];
                    var allMoons = moons.Where(x => x != moon);
                    moon.DetermineAndSaveVelocity(new Coordinate(allMoons.Count(x => x.Coordinate.X > moon.Coordinate.X) - allMoons.Count(x => x.Coordinate.X < moon.Coordinate.X), allMoons.Count(x => x.Coordinate.Y > moon.Coordinate.Y) - allMoons.Count(x => x.Coordinate.Y < moon.Coordinate.Y), allMoons.Count(x => x.Coordinate.Z > moon.Coordinate.Z) - allMoons.Count(x => x.Coordinate.Z < moon.Coordinate.Z)));
                }

                foreach (var moon in moons)
                {
                    moon.ApplyVelocity();
                }
            }

            var totalEnergy = moons.Select(x => x.GetEnergy()).Sum();

            ResultPart1 = string.Empty;
        }
Example #2
0
        public override string Part2()
        {
            var Carts       = PuzzleInput.Replace("\r\n", "").Select((c, index) => new { c, index }).Where(d => d.c == '>' || d.c == 'v' || d.c == '<' || d.c == '^').Select((e, eindex) => new Cart(e.index, e.c, eindex + 1)).ToList();
            var coordinates = string.Empty;
            var crash       = false;

            while (!crash)
            {
                List <int> cartsTobeRemoved = new List <int>();
                foreach (var Cart in Carts.OrderBy(c => c.Index))
                {
                    if (Cart.Move().HasValue)
                    {
                        cartsTobeRemoved.AddRange(Carts.Where(c => c.Index == Cart.Index).Select(c => c.ID).ToList());
                    }
                    ;
                }
                Carts.RemoveAll(c => cartsTobeRemoved.Contains(c.ID));

                if (Carts.Count == 1)
                {
                    coordinates = GetCoordinates(Carts.First().Index);
                    crash       = true;
                }
                if (Test)
                {
                    Print();
                }
            }
            return(coordinates);
        }
Example #3
0
 public Day13()
 {
     //Test = true;
     PuzzleInput   = Test ? part2puzzle : puzzle;
     ID            = 13;
     Name          = "Day 13: Mine Cart Madness";
     trackLines    = PuzzleInput.Replace("\r\n", "").ToCharArray();
     originalTrack = PuzzleInput.Replace("\r\n", "").Replace(">", "-").Replace("<", "-").Replace("^", "|").Replace("v", "|").ToCharArray();
     stride        = PuzzleInput.Substring(0, PuzzleInput.IndexOf("\r\n")).Length;
 }
Example #4
0
        private void FillFirstSpaceWithPuzzleInput(Space firstSpace)
        {
            var stride    = firstSpace.Grid.GetLength(0);
            var onestring = PuzzleInput.Replace("\r\n", "").Replace(" ", "");

            for (int i = 0; i < onestring.Length; i++)
            {
                var row = (int)(i / stride);
                firstSpace.Grid[i % stride, row] = onestring[i] == '.' ? 0 : 1;
            }
        }
Example #5
0
        public override string Part1()
        {
            var Carts       = PuzzleInput.Replace("\r\n", "").Select((c, index) => new { c, index }).Where(d => d.c == '>' || d.c == 'v' || d.c == '<' || d.c == '^').Select((e, eindex) => new Cart(e.index, e.c, eindex + 1)).ToList();
            var coordinates = string.Empty;
            var crash       = false;

            while (!crash)
            {
                foreach (var Cart in Carts)
                {
                    if (Cart.Move().HasValue)
                    {
                        crash       = true;
                        coordinates = GetCoordinates(Cart.Index);
                    }
                    ;
                }
                if (Test)
                {
                    Print();
                }
            }
            return(coordinates);
        }
Example #6
0
        public override async Task Part2()
        {
            var removegtstSigns = PuzzleInput.Replace("<", "").Replace(">", "");
            var lines           = removegtstSigns.Split("\r\n").Select(x => x.Split(',').Select(y => new string(y.Trim().SkipWhile(z => z == '=').ToArray()))).Select(x => x.Select(y => y.Substring(y.IndexOf('=') + 1).Trim()));;
            var moonsLijst      = lines.Select(x => new Moon(Convert.ToInt32(x.First()), Convert.ToInt32(x.Skip(1).First()), Convert.ToInt32(x.Last())));
            var moons           = moonsLijst.ToList();
            var numberofMoons   = moonsLijst.Count();
            var width           = 25;
            var height          = 20;

            for (long step = 0; step < 300000; step++)
            {
                var bm = new int[width, height];
                //initialstate = true;
                Parallel.ForEach(moons, (moon) =>
                {
                    // var moon = moons[i];
                    moon.UpIndex();
                    var allMoons = moons.Where(x => x != moon).ToHashSet();
                    moon.DetermineAndSaveVelocity(new Coordinate(allMoons.Count(x => x.Coordinate.X > moon.Coordinate.X) - allMoons.Count(x => x.Coordinate.X < moon.Coordinate.X), allMoons.Count(x => x.Coordinate.Y > moon.Coordinate.Y) - allMoons.Count(x => x.Coordinate.Y < moon.Coordinate.Y), allMoons.Count(x => x.Coordinate.Z > moon.Coordinate.Z) - allMoons.Count(x => x.Coordinate.Z < moon.Coordinate.Z)));
                });

                Parallel.ForEach(moons, (moon) =>
                {
                    moon.ApplyVelocity();
                });
            }

            var indexList = new List <long>();
            var niceCoord = new Coordinate(0, 0, 0);

            for (int step = 0; step < 300000; step++)
            {
                if (niceCoord.X == 0)
                {
                    checkX(moons, niceCoord, step);
                }
                if (niceCoord.Y == 0)
                {
                    checkY(moons, niceCoord, step);
                }
                if (niceCoord.Z == 0)
                {
                    checkZ(moons, niceCoord, step);
                }

                if (niceCoord.HasAll)
                {
                    break;
                }
            }

            var listLong = new List <long>()
            {
                niceCoord.X, niceCoord.Y, niceCoord.Z
            };
            var output = listLong.Aggregate((x, y) => determineLCM(x, y));


            var test = indexList.GroupBy(x => x).Where(g => g.Count() == numberofMoons);

            var totalEnergy = moons.Select(x => x.GetEnergy()).Sum();

            ResultPart2 = "";
        }