Пример #1
0
        private static void ConnectPortalEdges(this PositionsGraph graph)
        {
            while (true)
            {
                var portalPositions = graph.AdjacencyList.Where(x => x.Key.Content.All(char.IsUpper) && !x.Key.Content.Equals("AA") && !x.Key.Content.Equals("ZZ")).ToDictionary(x => x.Key, x => x.Value);
                if (portalPositions.Count == 0)
                {
                    break;
                }

                var initialPortal = portalPositions.First();
                portalPositions.Remove(initialPortal.Key);
                var endPortal = portalPositions.Single(x => x.Key.Content.Equals(initialPortal.Key.Content));
                portalPositions.Remove(endPortal.Key);

                var initialPortalPositions = initialPortal.Value.ToList();
                var endPortalPositions     = endPortal.Value.ToList();
                for (var i = 0; i < initialPortalPositions.Count; i++)
                {
                    for (var j = 0; j < endPortalPositions.Count; j++)
                    {
                        graph.AddEdge(new Tuple <Position, Position>(initialPortalPositions[i], endPortalPositions[j]));
                    }
                }

                var valuesListsInitialPortal = graph.AdjacencyList.Where(x => x.Value.Contains(initialPortal.Key));
                var valuesListsEndPortal     = graph.AdjacencyList.Where(x => x.Value.Contains(endPortal.Key));

                foreach (var node in valuesListsInitialPortal)
                {
                    node.Value.Replace(initialPortal.Value, initialPortal.Key);
                }

                foreach (var node in valuesListsInitialPortal)
                {
                    node.Value.Replace(initialPortal.Value, initialPortal.Key);
                }

                graph.RemoveVertex(initialPortal.Key);
                graph.RemoveVertex(endPortal.Key);
            }

            //graph.RemoveVertex(graph.AdjacencyList.First(x => x.Key.Content.Equals("A")).Key);
            //graph.RemoveVertex(graph.AdjacencyList.Last(x => x.Key.Content.Equals("Z")).Key);
        }
Пример #2
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 '#':
                    case ' ':
                        break;

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

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

            foreach (var position in allPositions)
            {
                var adjacentPositions = GetAdjacentPositions(allPositions, position);
                if (adjacentPositions.Any(x => x.Content.Equals(".")))
                {
                    positions.AddVertex(position);
                    foreach (var adjacentPosition in adjacentPositions)
                    {
                        if (adjacentPosition.Content.Length == 1 && char.IsUpper(char.Parse(adjacentPosition.Content)) && !GetAdjacentPositions(allPositions, adjacentPosition).Any(x => x.Content.Equals(".")))
                        {
                            positions.AdjacencyList.Single(x => x.Key.Content.Equals(position.Content)).Key.Content += adjacentPosition.Content;
                            if (positions.AdjacencyList.Count(x => x.Key.Content.Equals(position.Content)) == 1)
                            {
                                positions.AdjacencyList.Single(x => x.Key.Content.Equals(position.Content)).Key.Content = String.Concat(positions.AdjacencyList.Single(x => x.Key.Content.Equals(position.Content)).Key.Content.OrderBy(c => c));
                            }
                        }
                        else
                        {
                            positions.AddEdge(new Tuple <Position, Position>(position, adjacentPosition));
                        }
                    }
                }
            }

            //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);
        }