private IEnumerable <Move> GetAvailableMoves() { for (RoomId roomId = 0; roomId != RoomId.Count; roomId++) { Room room = Rooms[(int)roomId]; if (room.CanMoveOut) { Amphipod occupant = room.Peek(); RoomId destinationRoomId = occupant.Home; Room destinationRoom = Rooms[(int)destinationRoomId]; if (destinationRoom.CanMoveIn && Hallway.IsRoomReachable(roomId, destinationRoomId, out int hallDistance)) { yield return(new Move() { FromRoom = roomId, ToRoom = destinationRoomId, Cost = (room.MoveOutDistance + destinationRoom.MoveInDistance + hallDistance) * occupant.MoveCost, }); } else { foreach ((HallId hallId, int distance) in Hallway.HallsAvailableFrom(roomId)) { yield return(new Move() { FromRoom = roomId, ToHall = hallId, Cost = (room.MoveOutDistance + distance) * occupant.MoveCost, }); } } } } for (HallId hallId = 0; hallId < HallId.Count; hallId++) { Amphipod?occupantNullable = Hallway.Positions[(int)hallId]; if (occupantNullable.HasValue) { Amphipod occupant = occupantNullable.Value; RoomId roomId = occupant.Home; Room room = Rooms[(int)roomId]; if (room.CanMoveIn && Hallway.IsRoomReachable(hallId, roomId, out int hallDistance)) { yield return(new Move() { FromHall = hallId, ToRoom = roomId, Cost = (hallDistance + room.MoveInDistance) * occupant.MoveCost, }); } } } }
public Room(Amphipod amphipod, int index, string content) { Amphipod = amphipod; Index = index; Content = content; Organized = content.ToCharArray().All(c => c == amphipod.Type); HasSpace = content.AsSpan().IndexOf('.') != -1; IsEmptyOrHasAllValidAmphipods = content.ToCharArray().All(c => c == '.' || c == amphipod.Type); HasAmpipodsOfIncorrectType = !IsEmptyOrHasAllValidAmphipods; }
static IEnumerable <(int Index, Room Target)> FindAmphipodsInHallway(Board board) { for (var i = 0; i < board.Hallway.Length; i++) { var c = board.Hallway[i]; if (c is not '.') { var room = board.Rooms[Amphipod.Parse(c)]; if (room.IsEmptyOrHasAllValidAmphipods && HallwayPathClear(board.Hallway, i, room.Index)) { yield return(i, room); } } } }
internal void Push(Amphipod amphipod) { this.canMoveIn = null; this.isSolved = null; this.occupants.Push(amphipod); }