|
| 1 | +defmodule RobotSimulator do |
| 2 | + defstruct direction: nil, position: nil |
| 3 | + |
| 4 | + @valid_directions [:north, :east, :south, :west] |
| 5 | + |
| 6 | + @doc """ |
| 7 | + Create a Robot Simulator given an initial direction and position. |
| 8 | +
|
| 9 | + Valid directions are: `:north`, `:east`, `:south`, `:west` |
| 10 | +
|
| 11 | + ## Examples |
| 12 | +
|
| 13 | + iex> RobotSimulator.create(:north, {0, 0}) |
| 14 | + { :ok, %RobotSimulator{ direction: :north, position: {0, 0} } } |
| 15 | +
|
| 16 | + iex> RobotSimulator.create(:invalid, {0, 0}) |
| 17 | + { :error, "invalid direction" } |
| 18 | +
|
| 19 | + iex> RobotSimulator.create(:north, "invalid") |
| 20 | + { :error, "invalid position" } |
| 21 | + """ |
| 22 | + @spec create(direction :: atom, position :: {integer, integer}) :: { atom, any } |
| 23 | + def create(direction \\ :north, position \\ {0, 0}) do |
| 24 | + { :ok, %RobotSimulator{} } |> place(position) |> orient(direction) |
| 25 | + end |
| 26 | + |
| 27 | + @doc """ |
| 28 | + Simulate the robot's movement given a string of instructions. |
| 29 | +
|
| 30 | + Valid instructions are: "R" (turn right), "L", (turn left), and "A" (advance) |
| 31 | +
|
| 32 | + ## Examples |
| 33 | +
|
| 34 | + iex> { :ok, robot } = RobotSimulator.create |
| 35 | + iex> robot |> RobotSimulator.simulate("RRLAAR") |
| 36 | + { :ok, %RobotSimulator{direction: :south, position: {2, 0}} } |
| 37 | +
|
| 38 | + iex> { :ok, robot } = RobotSimulator.create |
| 39 | + iex> robot |> RobotSimulator.simulate("RRLAUR") |
| 40 | + { :error, "invalid instruction" } |
| 41 | + """ |
| 42 | + @spec simulate(robot :: %RobotSimulator{}, instructions :: String.t ) :: { atom, any } |
| 43 | + def simulate(%RobotSimulator{} = robot, instructions) do |
| 44 | + instructions |> String.graphemes |> Enum.reduce({ :ok, robot }, &move/2) |
| 45 | + end |
| 46 | + |
| 47 | + defp orient({ :ok, %RobotSimulator{} = robot }, direction) when direction in @valid_directions do |
| 48 | + { :ok, %RobotSimulator{ robot | direction: direction } } |
| 49 | + end |
| 50 | + defp orient({ :error, _ } = error, _), do: error |
| 51 | + defp orient(_, _), do: { :error, "invalid direction" } |
| 52 | + |
| 53 | + defp place({ :ok, %RobotSimulator{} = robot }, { x, y } = position) when is_integer(x) and is_integer(y) do |
| 54 | + { :ok, %RobotSimulator{ robot | position: position } } |
| 55 | + end |
| 56 | + defp place({ :error, _ } = error, _), do: error |
| 57 | + defp place(_, _), do: { :error, "invalid position" } |
| 58 | + |
| 59 | + defp move("R", { :ok, %RobotSimulator{ direction: direction } } = robot), do: robot |> orient(direction |> rotate_right) |
| 60 | + defp move("L", { :ok, %RobotSimulator{ direction: direction } } = robot), do: robot |> orient(direction |> rotate_left) |
| 61 | + defp move("A", { :ok, %RobotSimulator{ direction: :north, position: { x, y } } } = robot), do: robot |> place({ x, y + 1 }) |
| 62 | + defp move("A", { :ok, %RobotSimulator{ direction: :east, position: { x, y } } } = robot), do: robot |> place({ x + 1, y }) |
| 63 | + defp move("A", { :ok, %RobotSimulator{ direction: :south, position: { x, y } } } = robot), do: robot |> place({ x, y - 1 }) |
| 64 | + defp move("A", { :ok, %RobotSimulator{ direction: :west, position: { x, y } } } = robot), do: robot |> place({ x - 1, y }) |
| 65 | + defp move(_, { :error, _ } = error), do: error |
| 66 | + defp move(_, _), do: { :error, "invalid instruction" } |
| 67 | + |
| 68 | + for [curr, right] <- @valid_directions |> Stream.cycle |> Enum.take(5) |> Enum.chunk(2, 1) do |
| 69 | + defp rotate_right(unquote(curr)), do: unquote(right) |
| 70 | + defp rotate_left(unquote(right)), do: unquote(curr) |
| 71 | + end |
| 72 | +end |
0 commit comments