From 596a77db59d7b2d98db89bc31bf595f248128208 Mon Sep 17 00:00:00 2001 From: hhlal Date: Tue, 14 May 2024 18:15:43 +0300 Subject: [PATCH] declare all paths --- examplr00.txt | 9 +++++-- maze/maze.go | 73 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/examplr00.txt b/examplr00.txt index b1f8a48..c2f43be 100644 --- a/examplr00.txt +++ b/examplr00.txt @@ -4,7 +4,12 @@ ##end 3 5 2 2 5 6 -1-3 +4 7 7 +5 8 9 1-2 2-3 -3-2 +1-3 +1-4 +4-5 +5-3 +5-2 diff --git a/maze/maze.go b/maze/maze.go index 672cd97..c51baea 100644 --- a/maze/maze.go +++ b/maze/maze.go @@ -18,11 +18,10 @@ type Room struct { distance int name string occupancies int - paths []string + paths []string } - var collection [][]string - +var collection [][]string // Load method to load the maze from input func Load(inputStream *bufio.Reader) *Maze { @@ -77,7 +76,6 @@ func Load(inputStream *bufio.Reader) *Maze { if len(parts) > 0 { maze.endRoom = parts[0] log.Println("End room:", maze.endRoom) - // Ensure the end room is loaded as a regular room if !maze.loadRooms(nextLine) { return nil } @@ -106,7 +104,6 @@ func Load(inputStream *bufio.Reader) *Maze { return maze } - func (maze *Maze) loadRooms(line string) bool { // Parse room data parts := strings.Fields(line) @@ -151,7 +148,7 @@ func (maze *Maze) mapFarm(line string) bool { log.Printf("Mapping connection between rooms: %s and %s\n", room1Name, room2Name) // Find room objects by name - var room1,room2 *Room + var room1, room2 *Room for i := range maze.rooms { if maze.rooms[i].name == room1Name { room1 = &maze.rooms[i] @@ -168,32 +165,58 @@ func (maze *Maze) mapFarm(line string) bool { } // Establish connection between rooms - // room1. = append(room1.connections, *room2) - // room2.connections = append(room2.connections, *room1) - collection = append(collection, []string{room1Name, room2Name}) + room1.paths = append(room1.paths, room2Name) + room2.paths = append(room2.paths, room1Name) return true } func (maze *Maze) Solve() { log.Println("Solving the maze...") - //l want to declare all paths - var path string; - var currentcol = collection[0][0] - var flage bool - for !strings.HasPrefix(currentcol, maze.endRoom){ + var paths []string - if strings.HasPrefix(currentcol, maze.startRoom) { - path = currentcol - flage = true - break - } - - if flage { - + for _, room := range maze.rooms { // Iterate through rooms to find paths + if room.name == maze.startRoom { + maze.explorePaths(room, maze.endRoom, []string{}, &paths) + } + } + + if len(paths) == 0 { + log.Println("No paths found from start room to end room.") + } else { + log.Println("Paths found:") + for i, path := range paths { + log.Printf("Path %d: %s", i+1, path) } } - - log.Println(collection) - +} + +func (maze *Maze) explorePaths(currentRoom Room, endRoom string, currentPath []string, paths *[]string) { + currentPath = append(currentPath, currentRoom.name) // is the end room + + if currentRoom.name == endRoom { // is the end room + *paths = append(*paths, strings.Join(currentPath, " -> ")) + return + } + + // Explore all possible paths from current room + for _, nextRoomName := range currentRoom.paths { + for _, room := range maze.rooms { + if room.name == nextRoomName { + // Explore next room if it's not already visited + if !contains(currentPath, room.name) { + maze.explorePaths(room, endRoom, currentPath, paths) + } + } + } + } +} + +func contains(path []string, room string) bool { + for _, r := range path { + if r == room { + return true + } + } + return false }