Non recursive Queue/Tree solution implementation
This commit is contained in:
		
							parent
							
								
									fe768dadc4
								
							
						
					
					
						commit
						5644b23e33
					
				| @ -64,18 +64,7 @@ func Run(BotToken string) { | |||||||
|                             outputmaze.EditMaze(points, "/tmp/maze.png", "/tmp/outputmaze.png") |                             outputmaze.EditMaze(points, "/tmp/maze.png", "/tmp/outputmaze.png") | ||||||
|             }}}} |             }}}} | ||||||
| 
 | 
 | ||||||
|             if responseData != "" { |             if responseData == "" { | ||||||
|                 err = s.InteractionRespond( |  | ||||||
|                     i.Interaction, |  | ||||||
|                     &discordgo.InteractionResponse{ |  | ||||||
|                         Type: discordgo.InteractionResponseChannelMessageWithSource, |  | ||||||
|                         Data: &discordgo.InteractionResponseData{ |  | ||||||
|                             Flags: 1 << 6, |  | ||||||
|                             Content: responseData, |  | ||||||
|                         }, |  | ||||||
|                     }, |  | ||||||
|                 ) |  | ||||||
|             } else { |  | ||||||
|                 fileName := "/tmp/outputmaze.png" |                 fileName := "/tmp/outputmaze.png" | ||||||
|                 f, _ := os.Open(fileName) |                 f, _ := os.Open(fileName) | ||||||
|                 defer f.Close() |                 defer f.Close() | ||||||
| @ -93,12 +82,27 @@ func Run(BotToken string) { | |||||||
|                         }, |                         }, | ||||||
|                     }, |                     }, | ||||||
|                 ) |                 ) | ||||||
|             } |                 if err != nil { | ||||||
|             if err != nil { |                     if strings.Contains(err.Error(), "413") { | ||||||
|                 fmt.Println(err) |                         responseData = "Solution file is too large!" | ||||||
|  |                     } else { | ||||||
|  |                         responseData = err.Error() | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             if responseData != "" { | ||||||
|  |                 err = s.InteractionRespond( | ||||||
|  |                     i.Interaction, | ||||||
|  |                     &discordgo.InteractionResponse{ | ||||||
|  |                         Type: discordgo.InteractionResponseChannelMessageWithSource, | ||||||
|  |                         Data: &discordgo.InteractionResponseData{ | ||||||
|  |                             Flags: 1 << 6, | ||||||
|  |                             Content: responseData, | ||||||
|  |                         }, | ||||||
|  |                     }, | ||||||
|  |                 ) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     }) |     }}) | ||||||
| 
 | 
 | ||||||
|     discord.AddHandler(func ( |     discord.AddHandler(func ( | ||||||
|         s *discordgo.Session, |         s *discordgo.Session, | ||||||
| @ -141,7 +145,11 @@ func Run(BotToken string) { | |||||||
|                                 }, |                                 }, | ||||||
|                             }) |                             }) | ||||||
|                             if err != nil { |                             if err != nil { | ||||||
|                                 fmt.Println(err) |                                 if strings.Contains(err.Error(), "413") { | ||||||
|  |                                     s.ChannelMessageSendReply(m.ChannelID, "Solution file is too large!", m.Reference()) | ||||||
|  |                                 } else { | ||||||
|  |                                     s.ChannelMessageSendReply(m.ChannelID, err.Error(), m.Reference()) | ||||||
|  |                                 } | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|  | |||||||
| @ -22,7 +22,8 @@ Pixel types: | |||||||
| type Point struct { | type Point struct { | ||||||
|     X int; |     X int; | ||||||
|     Y int; |     Y int; | ||||||
|     Value int |     Value int; | ||||||
|  |     Parent *Point | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type Maze [][]Point | type Maze [][]Point | ||||||
|  | |||||||
| @ -2,12 +2,14 @@ package solvemaze | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"mazesolver/internal/getmaze" | 	"mazesolver/internal/getmaze" | ||||||
|  | 	"mazesolver/util/queue" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func FindPath(maze getMaze.Maze) []getMaze.Point { | func FindPath(maze getMaze.Maze) []getMaze.Point { | ||||||
|     returnSlice := make([]getMaze.Point, 0) |     returnSlice := make([]getMaze.Point, 0) | ||||||
|     debugMaze := GetDebugMaze(maze) |     //debugMaze := GetDebugMaze(maze)
 | ||||||
|     findPathRecursive(maze[1][1], maze, &returnSlice, debugMaze) |     //findPathRecursive(maze[1][1], maze, &returnSlice, debugMaze)
 | ||||||
|  |     findPathNonRecursive(maze, maze[1][1], &returnSlice) | ||||||
|     return returnSlice |     return returnSlice | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -49,3 +51,42 @@ func GetDebugMaze(maze getMaze.Maze) getMaze.Maze { | |||||||
|     } |     } | ||||||
|     return a |     return a | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func findPathNonRecursive(maze getMaze.Maze, root getMaze.Point, slice *[]getMaze.Point) { | ||||||
|  |     next := buildMaze(maze, root) | ||||||
|  |     for next != nil { | ||||||
|  |         *slice = append(*slice, *next) | ||||||
|  |         next = next.Parent | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func buildMaze(maze getMaze.Maze, root getMaze.Point) *getMaze.Point { | ||||||
|  |     Q := make(queue.Queue[getMaze.Point], 0) | ||||||
|  |     root.Value = -1 | ||||||
|  |     Q.Enqueue(root) | ||||||
|  |     for len(Q) > 0 { | ||||||
|  |         v := Q.Dequeue() | ||||||
|  |         if v.Value == 9 { | ||||||
|  |             return (&v) | ||||||
|  |         } | ||||||
|  |         for _, w := range getAdjacent(v, maze) { | ||||||
|  |             if (w.Value == 0 || w.Value == 9) { | ||||||
|  |                 maze[w.Y][w.X].Value = -1 | ||||||
|  |                 w.Parent = &v | ||||||
|  |                 Q.Enqueue(w) | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func contains(maze getMaze.Maze, val int) bool { | ||||||
|  |     for _, v := range maze { | ||||||
|  |         for _, vv := range v { | ||||||
|  |             if vv.Value == val { | ||||||
|  |                 return true | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     return false | ||||||
|  | } | ||||||
|  | |||||||
| @ -7,18 +7,18 @@ type QueueInterface interface { | |||||||
| 
 | 
 | ||||||
| type Queue[T any] []T | type Queue[T any] []T | ||||||
| 
 | 
 | ||||||
| func (q Queue[T]) Enqueue(v T) { | func (q *Queue[T]) Enqueue(v T) { | ||||||
|     q = append(q, v) |     *q = append(*q, v) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q Queue[T]) Dequeue() T { | func (q *Queue[T]) Dequeue() T { | ||||||
|     x := q[0] |     x := (*q)[0] | ||||||
|     q = q[1:] |     *q = (*q)[1:] | ||||||
|     return x |     return x | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (q Queue[T]) IsEmpty() bool { | func (q *Queue[T]) IsEmpty() bool { | ||||||
|     if len(q) == 0 { |     if len(*q) == 0 { | ||||||
|         return true |         return true | ||||||
|     } |     } | ||||||
|     return false; |     return false; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 semblanceofsense
						semblanceofsense