예제 #1
0
        private static PositionsGraph ReadInput(string[] input)
        {
            var             positions    = new PositionsGraph();
            List <Position> allPositions = new List <Position>();
            int             x            = 0;
            int             y            = 0;

            foreach (var line in input)
            {
                foreach (var character in line)
                {
                    switch (character)
                    {
                    case '#':
                        break;

                    case '.':
                        allPositions.Add(new Position(x, y, ' '));
                        break;

                    default:
                        allPositions.Add(new Position(x, y, character));
                        break;
                    }
                    x++;
                }
                x = 0;
                y++;
            }

            foreach (var position in allPositions)
            {
                positions.AddVertex(position);
                var adjacentPositions = GetAdjacentPositions(allPositions, position);
                foreach (var adjacentPosition in adjacentPositions)
                {
                    positions.AddEdge(new Tuple <Position, Position>(position, adjacentPosition));
                }
            }

            return(positions);
        }
예제 #2
0
        private static List <Key> GetKeysDistances(PositionsGraph graph, Position position, string toExclude)
        {
            var toReturn = new List <Key>();

            var allKeys = graph.GetAllKeys().Where(x => !x.Content.ToString().Equals(toExclude)).ToList();

            foreach (var key in allKeys)
            {
                var path = graph.BFSShortestPath(position, key);
                if (path == null)
                {
                    continue;
                }
                var doors       = path.Where(x => char.IsUpper(x.Content)).ToList();
                var keyToCreate = new Key(key.X, key.Y, key.Content, path.Count - 1, doors);
                toReturn.Add(keyToCreate);
            }

            return(toReturn);
        }