Exemplo n.º 1
0
        public static string B(string input)
        {
            List <string> inputList = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
            IntVector2    position  = new IntVector2(0, 0);
            IntVector2    waypoint  = new IntVector2(10, -1);

            foreach (string line in inputList)
            {
                char direction = line[0];
                int  magnitude = Convert.ToInt32(line.Substring(1));
                switch (direction)
                {
                case 'N':
                    waypoint = waypoint.North(magnitude);
                    break;

                case 'E':
                    waypoint = waypoint.East(magnitude);
                    break;

                case 'S':
                    waypoint = waypoint.South(magnitude);
                    break;

                case 'W':
                    waypoint = waypoint.West(magnitude);
                    break;

                case 'F':
                    position = position.Add(waypoint.Multiply(magnitude));
                    break;

                case 'L':
                    while (magnitude > 0)
                    {
                        waypoint   = waypoint.Left();
                        magnitude -= 90;
                    }
                    break;

                case 'R':
                    while (magnitude > 0)
                    {
                        waypoint   = waypoint.Right();
                        magnitude -= 90;
                    }
                    break;

                default:
                    throw new Exception("Invalid direction: " + direction);
                }
                //Console.WriteLine("Position: " + position + ", Direction: " + facing);
            }
            return(position.Distance(new IntVector2(0, 0)).ToString());;
        }
Exemplo n.º 2
0
        public static string B(string input)
        {
            seaMonster.Add(new IntVector2(18, 0));
            seaMonster.Add(new IntVector2(0, 1));
            seaMonster.Add(new IntVector2(5, 1));
            seaMonster.Add(new IntVector2(6, 1));
            seaMonster.Add(new IntVector2(11, 1));
            seaMonster.Add(new IntVector2(12, 1));
            seaMonster.Add(new IntVector2(17, 1));
            seaMonster.Add(new IntVector2(18, 1));
            seaMonster.Add(new IntVector2(19, 1));
            seaMonster.Add(new IntVector2(1, 2));
            seaMonster.Add(new IntVector2(4, 2));
            seaMonster.Add(new IntVector2(7, 2));
            seaMonster.Add(new IntVector2(10, 2));
            seaMonster.Add(new IntVector2(13, 2));
            seaMonster.Add(new IntVector2(16, 2));

            ProcessInput(input);
            LockSectors();

            // find the corners
            // first, find the north west corner
            IntVector2 currentPos = new IntVector2(0, 0);

            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.North();
            }
            currentPos = currentPos.South();
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.West();
            }
            currentPos = currentPos.East();

            int minX = currentPos.X;
            int minY = currentPos.Y;

            // Find the south east corner;
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.South();
            }
            currentPos = currentPos.North();
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.East();
            }
            currentPos = currentPos.West();
            int maxX = currentPos.X;
            int maxY = currentPos.Y;

            // Make the map
            char[,] map = new char[(maxX - minX + 1) * 8, (maxY - minY + 1) * 8];
            for (int outerX = minX; outerX <= maxX; outerX++)
            {
                for (int outerY = minY; outerY <= maxY; outerY++)
                {
                    char[,] contents = lockedSectors[new IntVector2(outerX, outerY)].Contents;
                    for (int x = 0; x < 8; x++)
                    {
                        for (int y = 0; y < 8; y++)
                        {
                            map[(outerX - minX) * 8 + x, (outerY - minY) * 8 + y] = contents[x + 1, y + 1];
                        }
                    }
                }
            }

            // Map made. Now we need to find sea monsters
            int size            = map.GetLength(0);
            int seaMonsterCount = 0;

            for (int i = 0; i < 8; i++)
            {
                seaMonsterCount = CountSeaMonsters(map);
                if (seaMonsterCount > 0)
                {
                    break;
                }
                // (x,y) -> (height-y, x)
                char[,] newMap = new char[size, size];
                for (int x = 0; x < size; x++)
                {
                    for (int y = 0; y < size; y++)
                    {
                        newMap[x, y] = map[size - y - 1, x];
                    }
                }
                map = newMap;
                if (i == 4)
                {
                    newMap = new char[size, size];
                    for (int x = 0; x < size; x++)
                    {
                        for (int y = 0; y < size; y++)
                        {
                            newMap[x, y] = map[y, x];
                        }
                    }
                    map = newMap;
                }
            }

            // Sea monsters are 15 squares

            // count the number of waves
            int waveCount = 0;

            for (int x = 0; x < size; x++)
            {
                for (int y = 0; y < size; y++)
                {
                    if (map[x, y] == '#')
                    {
                        waveCount++;
                    }
                }
            }

            return((waveCount - 15 * seaMonsterCount).ToString());
        }
Exemplo n.º 3
0
        public static string A(string input)
        {
            ProcessInput(input);
            LockSectors();
            //DrawSectors();

            /*
             * for (int y = -5; y <= 5; y++)
             * {
             *  for (int x = -5; x <= 5; x++)
             *  {
             *      if (lockedSectors.ContainsKey(new IntVector2(x, y)))
             *      {
             *          Console.Write("X");
             *      }
             *      else
             *      {
             *          Console.Write(".");
             *      }
             *  }
             *  Console.WriteLine();
             * }
             */
            // find the corners
            // first, find the north west corner
            long       answer     = 1;
            IntVector2 currentPos = new IntVector2(0, 0);

            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.North();
            }
            currentPos = currentPos.South();
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.West();
            }
            currentPos = currentPos.East();
            answer     = answer * lockedSectors[currentPos].Id;

            // move to the north east corner
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.East();
            }
            currentPos = currentPos.West();
            answer     = answer * lockedSectors[currentPos].Id;

            // move to the south east corner
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.South();
            }
            currentPos = currentPos.North();
            answer     = answer * lockedSectors[currentPos].Id;

            // move to the south west corner
            while (lockedSectors.ContainsKey(currentPos))
            {
                currentPos = currentPos.West();
            }
            currentPos = currentPos.East();
            answer     = answer * lockedSectors[currentPos].Id;

            return(answer.ToString());
        }