Reference
How the cart works
Driving a cart with code is just a position and a facing. Once you can picture which cell the cart is on and which way it points, you can read a program and trace its route around the walls before it runs. Here’s the model. Open the track lab →
- The cart has a cell and a facing
The cart sits on one square of the grid and points one way (up, down, left, or right). Everything it does is relative to which cell it is on and which way it faces right now.
- move() goes forward one cell
move() sends the cart one square the way it faces — but only if that square is open. If a wall or the edge is in the way, the cart stays put (the move is blocked).
- turns change the facing, not the cell
turnLeft() and turnRight() rotate the cart in place by 90°. They don’t move it — they aim the NEXT move(). To go around a corner: move up to it, turn, then move on.
- canMove() looks one cell ahead
canMove() answers yes/no: is the cell right in front open? That lets a program decide — `while canMove() { move() }` drives forward until a wall or edge stops it, with no counting.
- A rule can steer around walls
An if/else rule each step — “if you can move, do; otherwise turn” — follows a bent corridor without knowing its shape in advance. That is how one small program solves many tracks.
- A solver proves it’s solvable
Before you ever see a track, a search (breadth-first) tries every route and checks that SOME program reaches the ore — so a puzzle is never impossible — and it reports the shortest program, the “par”.
The cart commands
move()Go forward one cell (if it is open); otherwise stay put.turnLeft()Rotate 90° left, in place — aims the next move.turnRight()Rotate 90° right, in place — aims the next move.canMove()Yes/no: is the cell straight ahead open (not a wall or the edge)?atOre()Yes/no: is the cart on the ore right now?while canMove() { … }Repeat while the way ahead is open — drive until something blocks it.
The trick to reading a cart program is to trace it cell by cell: move forward, turn to aim, check ahead, repeat. Walls and the edge stop a move; the loop or the rule steers around them. Read a cart program →