Solving the lem-in Problem in Go: Building a Digital Ant Farm

Oct 3, 2023

Lem-in algorithm visualisation on graph
Lem-in algorithm visualisation on graph


Solving the lem-in problem in Go was a rewarding experience.The lem-in algorithm, which is designed to find the shortest path for moving ants through a maze of rooms and tunnels, can be applied to real-life scenarios that involve optimizing transportation or routing. Here are a few practical use cases where the lem-in algorithm or similar graph-based pathfinding algorithms can be useful:

  • GPS Navigation Systems: GPS navigation apps like Google Maps use pathfinding algorithms to calculate the shortest or fastest route between two locations, considering real-time traffic data.
  • Logistics and Supply Chain Management: Logistics companies employ pathfinding algorithms to optimize the delivery routes for trucks or delivery drones, minimizing fuel costs and delivery times.
  • Public Transportation Planning:Public transportation authorities use pathfinding algorithms to plan bus, subway, or train routes, ensuring efficient and convenient transit for passengers.
  • Network Routing: Internet routers use routing algorithms to forward data packets through the network along the most efficient paths to their destinations.
  • Game Development: Video game developers use pathfinding algorithms to create realistic AI behavior for non-player characters (NPCs) or enemies that navigate game environments.
  • Autonomous Vehicles: Self-driving cars and drones use pathfinding algorithms to plan routes, avoid obstacles, and safely reach their destinations.
  • Emergency Response Planning: During natural disasters or emergencies, pathfinding algorithms help first responders find the fastest routes to reach affected areas or evacuate residents.
  • Robot Warehousing: In automated warehouses, robots use pathfinding algorithms to efficiently pick up and deliver items, optimizing order fulfillment.
  • Traffic Management: Traffic management systems use pathfinding algorithms to control traffic signals and optimize traffic flow in real time.
  • Resource Allocation: Organizations use pathfinding algorithms to allocate resources, assign tasks to workers, or distribute goods efficiently.


In Lem-in, you'll be provided with a map that may contain numerous interconnected rooms. The map will feature a designated starting room and an endpoint. Your task is to discover all valid paths, from the shortest to the longest. Additionally, you're responsible for guiding 'ants' through all the paths you uncover. However, it's imperative to ensure that you transport all the ants from the starting point to the endpoint in the most efficient manner possible. While the starting room and endpoint can accommodate an unlimited number of ants, each of the intermediary rooms can only host one ant at any given time.

Understanding the Problem

The goal of lem-in is to move a group of ants from a starting point to an endpoint in a virtual ant farm. The farm consists of rooms connected by tunnels, and each room can hold at most one ant at a time. To solve this problem, we need to find the optimal path to minimize the time it takes for the ants to reach their destination.If you want understand more about the lem-in algorithm you can read this article **.**

Error Handling

Before diving into problem-solving, it's crucial to handle input data errors correctly. We've implemented checks to ensure that the input file is valid. If the data format doesn't match the specifications, the program returns informative error messages to guide the user.

Graph Construction

The fundamental approach to solving lem-in involves modeling the ant farm as a graph. We accomplished this by organizing the farm's layout into interconnected rooms and tunnels. To ascertain the viable routes between these rooms, we harnessed the power of the depth-first search (DFS) algorithm, incorporating backtracking techniques. This strategic approach enabled us to craft a visual representation of the labyrinth, significantly streamlining the quest for optimal pathways. Importantly, the graph in this context is non-weighted, offering the flexibility to employ algorithms that best suit the specific requirements of solving lem-in puzzles.

Instantaneous Weighting

An essential stride in problem resolution involves the assignment of path weights, contingent on the quantity of ants and their velocity. We adopted an instantaneous weighting strategy to enhance ant maneuvering, all the while mitigating congestion. This entails the dynamic fine-tuning of path weights in response to the prevailing ant volume.

Ant Movement

Ant movement was managed to respect the order of arrival and avoid conflicts. Each ant was directed along the shortest path to its destination, ensuring it passed through a tunnel only once. Ants were moved sequentially, considering available path choices.

Go Server and Visualization

As an added feature, we developed an API backend and integrated it with the D3.js library for visualizing ant movements. This innovative approach enabled us to showcase real-time ant navigation on an interactive web page. You can now track the journey of each individual ant within the virtual ant farm with a sense of wonder.


In conclusion, the lem-in project is an excellent opportunity to explore algorithms, graph manipulation, and solving complex problems. By using the Go language, we successfully created a functional and optimized solution for guiding ants through a digital ant farm. We hope this article has inspired you to take on similar challenges and explore Go's capabilities in solving complex problems.

Code source available on github: here