Exemplo n.º 1
0
        public string Solve(string input)
        {
            string[] inputLines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            int    yOffset = inputLines[0].Length;
            string map     = string.Concat(inputLines);

            // Find the minecarts
            List <Minecart> minecarts = Minecart.FindInMap(ref map, yOffset);

            while (true)
            {
                // First, sort the minecarts by their position...
                minecarts = minecarts.OrderBy(x => x.Position).ToList();

                // Now move them all...
                foreach (Minecart current in minecarts)
                {
                    current.Position += current.NextMove;
                    current.SetNextMove(yOffset, map[current.Position]);

                    // Has it crashed?
                    if (current.GetCrashedIntoMinecart(minecarts) != null)
                    {
                        return($"{current.Position % yOffset},{current.Position / yOffset}");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public string Solve(string input)
        {
            string[] inputLines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            int    yOffset = inputLines[0].Length;
            string map     = string.Concat(inputLines);

            // Find the minecarts
            List <Minecart> minecarts = Minecart.FindInMap(ref map, yOffset);

            // We won't be able to just remove carts from the collection as we go,
            // so we keep a list of those that have been removed
            var removedCarts = new List <Minecart>();

            while (true)
            {
                // First, sort the minecarts by their position...
                minecarts = minecarts.OrderBy(x => x.Position).ToList();

                // Now move the ones that haven't crashed...
                foreach (Minecart current in minecarts)
                {
                    if (removedCarts.Contains(current))
                    {
                        continue;
                    }

                    current.Position += current.NextMove;
                    current.SetNextMove(yOffset, map[current.Position]);

                    // Has it crashed?
                    Minecart crashedIntoMinecart = current.GetCrashedIntoMinecart(minecarts.Where(x => !removedCarts.Contains(x)));

                    if (crashedIntoMinecart != null)
                    {
                        removedCarts.Add(current);
                        removedCarts.Add(crashedIntoMinecart);
                    }
                }

                // At the end of the tick - is there only one remaining?
                if (minecarts.Count - removedCarts.Count == 1)
                {
                    Minecart lastCart = minecarts.First(x => !removedCarts.Contains(x));
                    return($"{lastCart.Position % yOffset},{lastCart.Position / yOffset}");
                }
            }
        }
 public static void TurnLeft(this Minecart cart, int yOffset)
 {
     if (cart.NextMove == -1)
     {
         cart.NextMove = yOffset;
     }
     else if (cart.NextMove == 1)
     {
         cart.NextMove = -yOffset;
     }
     else if (cart.NextMove == yOffset)
     {
         cart.NextMove = 1;
     }
     else
     {
         cart.NextMove = -1;
     }
 }
        public static void SetNextMove(this Minecart cart, int yOffset, char currentRail)
        {
            // Now work out where it'll go next
            // We only care if it's on something that'll change it's direction...
            if (currentRail == '/' && (cart.NextMove == -1 || cart.NextMove == 1))
            {
                // Coming to a left turn
                cart.TurnLeft(yOffset);
            }
            else if (currentRail == '/')
            {
                // Coming to a right turn
                cart.TurnRight(yOffset);
            }
            else if (currentRail == '\\' && (cart.NextMove == -1 || cart.NextMove == 1))
            {
                // Coming to a right turn from the left
                cart.TurnRight(yOffset);
            }
            else if (currentRail == '\\')
            {
                // Coming to a left turn from the bottom
                cart.TurnLeft(yOffset);
            }
            else if (currentRail == '+')
            {
                switch (cart.NextIntersectionBehavior % 3)
                {
                case 0:
                    cart.TurnLeft(yOffset);
                    break;

                case 2:
                    cart.TurnRight(yOffset);
                    break;
                }

                cart.NextIntersectionBehavior++;
            }
        }
 public static Minecart GetCrashedIntoMinecart(this Minecart cart, IEnumerable <Minecart> otherMinecarts)
 {
     return(otherMinecarts.FirstOrDefault(x => x != cart && x.Position == cart.Position));
 }
 public static void TurnRight(this Minecart cart, int yOffset)
 {
     cart.TurnLeft(yOffset);
     cart.NextMove *= -1;
 }