Пример #1
0
        private void DrawMap(Dictionary <Coordinate, char> map, Droid droid)
        {
            var minX = map.Keys.Select(coord => coord.X).Append(droid.CurrentLocation.X).Min() - 5;
            var minY = map.Keys.Select(coord => coord.Y).Append(droid.CurrentLocation.Y).Min() - 5;
            var maxX = map.Keys.Select(coord => coord.X).Append(droid.CurrentLocation.X).Max() + 5;
            var maxY = map.Keys.Select(coord => coord.Y).Append(droid.CurrentLocation.Y).Max() + 5;

            for (int y = minY; y < maxY; y++)
            {
                for (int x = minX; x < maxX; x++)
                {
                    var coord = Coordinate.Create(x, y);
                    if (coord.Equals(droid.CurrentLocation))
                    {
                        Console.Write('D');
                    }
                    else if (map.ContainsKey(coord))
                    {
                        Console.Write(map[coord]);
                    }
                    else
                    {
                        Console.Write(' ');
                    }
                }
                Console.WriteLine();
            }
        }
Пример #2
0
        public object Work1()
        {
            var comp  = new IntCodeComp(input);
            var droid = new Droid(comp);

            var map = new Dictionary <Coordinate, char>
            {
                { droid.CurrentLocation, 'S' }
            };

            droid.Start();

            int distance = FindOxygenLocation(droid, map);

            var oxygenCoord = map.Single(m => m.Value == '@').Key;

            DrawMap(map, droid);

            return(distance);
        }
Пример #3
0
        private int FindOxygenLocation(Droid droid, Dictionary <Coordinate, char> map)
        {
            AddIfNotExists(droid.CurrentLocation, 0);

            int oxygenDistance = -1;

            while (oxygenDistance == -1 || map.Count < 1650)
            {
                var east  = Coordinate.Create(droid.CurrentLocation.X + 1, droid.CurrentLocation.Y);
                var west  = Coordinate.Create(droid.CurrentLocation.X - 1, droid.CurrentLocation.Y);
                var north = Coordinate.Create(droid.CurrentLocation.X, droid.CurrentLocation.Y - 1);
                var south = Coordinate.Create(droid.CurrentLocation.X, droid.CurrentLocation.Y + 1);

                // explore unkown space
                Direction dir = Direction.None;
                if (!map.ContainsKey(east))
                {
                    dir = Direction.East;
                }
                else if (!map.ContainsKey(south))
                {
                    dir = Direction.South;
                }
                else if (!map.ContainsKey(north))
                {
                    dir = Direction.North;
                }
                else if (!map.ContainsKey(west))
                {
                    dir = Direction.West;
                }

                // trace back through known territory
                if (dir == Direction.None)
                {
                    if (map[east] != '#')
                    {
                        dir = Direction.East;
                    }
                    else if (map[south] != '#')
                    {
                        dir = Direction.South;
                    }
                    else if (map[west] != '#')
                    {
                        dir = Direction.West;
                    }
                    else if (map[north] != '#')
                    {
                        dir = Direction.North;
                    }

                    // add previous visits to path calculation
                    var surroundings = new Dictionary <Coordinate, int>();
                    if (droid.VisitedCoords.ContainsKey(east))
                    {
                        surroundings.Add(east, droid.VisitedCoords[east]);
                    }
                    if (droid.VisitedCoords.ContainsKey(west))
                    {
                        surroundings.Add(west, droid.VisitedCoords[west]);
                    }
                    if (droid.VisitedCoords.ContainsKey(north))
                    {
                        surroundings.Add(north, droid.VisitedCoords[north]);
                    }
                    if (droid.VisitedCoords.ContainsKey(south))
                    {
                        surroundings.Add(south, droid.VisitedCoords[south]);
                    }

                    if (surroundings.Values.Distinct().Count() > 1)
                    {
                        int leastVisited      = surroundings.Values.Min();
                        var leastVisitedCoord = surroundings.First(s => s.Value == leastVisited).Key;

                        if (leastVisitedCoord == east)
                        {
                            dir = Direction.East;
                        }
                        else if (leastVisitedCoord == west)
                        {
                            dir = Direction.West;
                        }
                        else if (leastVisitedCoord == north)
                        {
                            dir = Direction.North;
                        }
                        else if (leastVisitedCoord == south)
                        {
                            dir = Direction.South;
                        }
                    }
                }

                int  lastDistance = distance[droid.CurrentLocation];
                char c            = droid.MoveDirection(dir);
                AddIfNotExists(droid.CurrentLocation, lastDistance + 1);

                if (c == '@')
                {
                    oxygenDistance = distance[droid.CurrentLocation];
                }

                if (!map.ContainsKey(droid.CurrentLocation))
                {
                    map.Add(droid.CurrentLocation, c);
                }
                else if (c == '#')
                {
                    switch (dir)
                    {
                    case Direction.North:
                        if (!map.ContainsKey(north))
                        {
                            map.Add(north, '#');
                        }
                        break;

                    case Direction.South:
                        if (!map.ContainsKey(south))
                        {
                            map.Add(south, '#');
                        }
                        break;

                    case Direction.West:
                        if (!map.ContainsKey(west))
                        {
                            map.Add(west, '#');
                        }
                        break;

                    case Direction.East:
                        if (!map.ContainsKey(east))
                        {
                            map.Add(east, '#');
                        }
                        break;

                    case Direction.None:
                    default:
                        throw new Exception("Invalid direction. This should never happen.");
                    }
                }

                /*Console.Clear();
                 * DrawMap(map, droid);*/
            }

            return(oxygenDistance);
        }