Spring Challenge 2023: Ants

2023

Contents
Problem
Two colonies of ants gather crystal on a map of hexagons. You can't move an ant; you place weighted markers called beacons and the ants spread out between them in proportion to the weights.
Hard limit
About 95 ms of each 100 ms turn. A strength for every cell of a large map is far too many choices to search one cell at a time, and each candidate needs the next 8 turns played out to be judged.
Approach
Search over routes, not cells. The bot grows shortest-path trees from its bases to the crystal and eggs, gives each path a weight, and tunes the weights with simulated annealing (a search that sometimes accepts a worse option, less often as time runs out, so it doesn't get stuck). Each plan is scored on a copy of the referee's rules.
Result
53rd of 5,792 on the game's leaderboard, in the top (Legend) league.
What it shows
Reshaping a problem so a time-limited search can work on it, and keeping a faithful model of the rules to judge plans against. The page's own simulator reproduces every board the bots were sent in these games.
In the replay
Both bots' beacons are drawn, thicker for stronger, along with the chains of ants that harvested. The three games are ranked battles that my bot won.

Replays

Below are three recorded ranked battles of my bot against players near the top of the leaderboard. The page replays the beacons that both bots printed. It doesn’t run any search. Ant movement, spawning and harvesting are simulated in the browser from the game rules. For every board that either bot was sent in these three games, the simulation gives the same ant counts, crystal and scores, and it finishes on the same final scores that CodinGame reported.

Keyboard: focus the map, then use the left and right arrows to step, space to play or pause, and Home or End to jump to the start or finish. The animation starts by itself unless your system asks for reduced motion.

Each turn shows the board my bot was sent, the chains that harvested on that turn, and the beacons both bots placed in reply. Beacon outlines get thicker with strength. The sizes of the crystal and egg icons show how much is left in the cell.

These are ranked battles from December 2026, played by the leaderboard’s matchmaking. The opponents are the players now ranked 24th and 55th; their bots may have changed since.

The rules

Ants is the game from CodinGame’s Spring Challenge 2023. Two players each run a colony of ants on a map of hexagonal cells. You can’t move an ant. Instead you place beacons on cells, each with a strength, and your ants split between the beacons in proportion to those strengths and walk towards them one cell per turn.

Points come from harvesting. A cell with crystal scores for you when an unbroken chain of your ants links it to one of your bases, and the score per turn is the number of ants on the thinnest cell of the chain. Egg cells work the same way but spawn new ants at your bases instead of scoring. If the opponent has more ants on a cell, with a chain back to their own base, your chain is cut there. The game ends when one player has more than half of the crystal, or after 100 turns.

How my bot works

The bot is written in Rust. Each turn it searches for a set of beacons, using a copy of the referee’s rules to try each candidate, and has about 95 ms of the 100 ms limit to do it. It keeps nothing from the previous turn’s search.

Candidate beacon paths. Beacons placed one at a time on a map this size are hopeless to search, so the bot searches over routes. It builds a greedy tree outward from my bases, repeatedly adding the shortest link from a cell already in the tree to a resource cell that isn’t, and it does that three times: for all resources together, for eggs only and for crystal only. The links from all three trees are the candidate edges. Each edge is a shortest path of cells, and a plan gives it a weight from 0 to 15, which becomes the beacon strength on every cell of that path. Weight 0 switches the edge off.

Scoring a plan. The bot plays the next 8 turns of the game on its own copy of the board, with the referee’s ant allocation and the harvest rules, including the opponent’s chains cutting mine. Only my ants move in this copy; the opponent’s ants are assumed to stay where they are. Each turn’s change in ants and crystal, mine minus the opponent’s, is added to the total, and later turns count for less (each is worth 0.7 times the one before). A plan that ends the game as a win or loss gets a bonus or penalty of 150, discounted the same way.

Eggs early, crystal late. The weights on ants and crystal shift as the game goes on. Early on, spawning ants is worth more. Later, crystal is. Game progress is the larger of the turn number out of 100 and the leading score as a share of half the crystal on the map.

Searching. The first stage is simulated annealing over the edge weights. Each step changes a few edges, either switching one off (60% of the time) or giving it a random weight, and keeps the change according to how the score moves and a falling temperature. Early in the turn it changes six edges at a time, and later it changes five and then two, so the search settles down as time runs out. The second stage takes the best plan and gives it a separate set of beacon strengths for each of the 8 turns, then mutates individual cells’ strengths for the rest of the time. Only the first turn’s beacons are printed.

If the best plan it finds has every edge switched off, the bot prints WAIT and its ants stay where they are. In the replays these are the turns where I set no beacons: the chains already harvesting are left alone.

Back to CodinGame and the other post-mortems.

Back to all projects Get in touch