TypeScript: A robot with exhaustive commands

Drive a robot on a grid with commands parsed into a discriminated union, and let TypeScript prove every command is handled.

What to build

A program that moves a robot and reports where it is.

Requirements

  • Parse each line into a Command or null: move N (N a whole number), turn left, turn right, report; anything else is invalid and prints bad command: LINE.
  • The robot starts at (0, 0) facing north. North increases y, east increases x.
  • report prints at (X, Y) facing DIR.
  • Handle the commands in a switch on kind whose default branch assigns the command to a never variable.

Input

Line 1: n. Then n lines.

Output

One line per report or bad command.

Constraints

1 ≤ n ≤ 100; 0 ≤ N ≤ 1000.

Example

Input:

5
move 2
turn right
move 3
report
jump

Output:

at (3, 2) facing east
bad command: jump

Why it works

The never check means that if a new kind of command is added to the union, the compiler points at the switch until it is handled, and the parser is the only place that deals with raw text.

Examples

Example 1 · Input
5
move 2
turn right
move 3
report
jump
Expected output
at (3, 2) facing east
bad command: jump
Example 2 · Input
4
turn left
turn left
move 5
report
Expected output
at (0, -5) facing south

Hints

Hint 1

type Command = { kind: "move"; steps: number } | { kind: "turn"; dir: "left" | "right" } | { kind: "report" }.

Hint 2

Keep the directions in an array ["north", "east", "south", "west"] and turn by moving the index.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

type Command = { kind: "move"; steps: number } | { kind: "turn"; dir: "left" | "right" } | { kind: "report" };

function parse(line: string): Command | null {
  const parts = line.split(" ");
  if (parts.length === 2 && parts[0] === "move" && /^\d+$/.test(parts[1])) return { kind: "move", steps: Number(parts[1]) };
  if (parts.length === 2 && parts[0] === "turn" && (parts[1] === "left" || parts[1] === "right")) return { kind: "turn", dir: parts[1] };
  if (line === "report") return { kind: "report" };
  return null;
}

const DIRECTIONS = ["north", "east", "south", "west"];
const DX = [0, 1, 0, -1];
const DY = [1, 0, -1, 0];
let x = 0;
let y = 0;
let facing = 0;

const n = Number(read());
for (let i = 0; i < n; i++) {
  const line = read();
  const command = parse(line);
  if (command === null) {
    console.log("bad command: " + line);
    continue;
  }
  switch (command.kind) {
    case "move":
      x += DX[facing] * command.steps;
      y += DY[facing] * command.steps;
      break;
    case "turn":
      facing = (facing + (command.dir === "right" ? 1 : 3)) % 4;
      break;
    case "report":
      console.log("at (" + x + ", " + y + ") facing " + DIRECTIONS[facing]);
      break;
    default: {
      const unhandled: never = command;
      throw new Error("unknown command " + JSON.stringify(unhandled));
    }
  }
}

Lesson: Narrowing: from a union to one type →