Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Last active November 8, 2019 08:41
Show Gist options
  • Save chgeuer/23043a22ef8999e1eab623f878430ea8 to your computer and use it in GitHub Desktop.
Save chgeuer/23043a22ef8999e1eab623f878430ea8 to your computer and use it in GitHub Desktop.

RobotJourney

Elixir

defmodule RobotJourney do
  # https://github.com/mikehadlow/Journeys

  @doc ~S"""
  ## Examples

      iex> RobotJourney.run("1 1 E", "RFRFRFRF")
      "1 1 E"

      iex> RobotJourney.run("3 2 N", "FRRFLLFFRRFLL")
      "3 3 N"

      iex> RobotJourney.run("0 3 W", "LLFFFLFLFL")
      "2 4 S"
  """
  def run(initial_pos, directions) when is_binary(initial_pos) and is_binary(directions),
    do:
      directions
      |> String.codepoints()
      |> List.foldl(parse_pos(initial_pos), &move/2)
      |> serialize_pos()

  defp move("L", {x, y, "N"}), do: {x, y, "W"}
  defp move("L", {x, y, "W"}), do: {x, y, "S"}
  defp move("L", {x, y, "S"}), do: {x, y, "E"}
  defp move("L", {x, y, "E"}), do: {x, y, "N"}
  defp move("R", {x, y, "N"}), do: {x, y, "E"}
  defp move("R", {x, y, "W"}), do: {x, y, "N"}
  defp move("R", {x, y, "S"}), do: {x, y, "W"}
  defp move("R", {x, y, "E"}), do: {x, y, "S"}
  defp move("F", {x, y, "N"}), do: {x, y + 1, "N"}
  defp move("F", {x, y, "W"}), do: {x - 1, y, "W"}
  defp move("F", {x, y, "S"}), do: {x, y - 1, "S"}
  defp move("F", {x, y, "E"}), do: {x + 1, y, "E"}

  defp serialize_pos({x, y, d}), do: "#{x} #{y} #{d}"
  defp parse_pos(pos) when is_binary(pos) do
    with [x, y, d] <- pos |> String.split(),
         {x, ""} <- x |> Integer.parse(),
         {y, ""} <- y |> Integer.parse() do
      {x, y, d}
    end
  end
end

C#

using System;
using System.Linq;

public static class RobotJourney
{
    static void Main(string[] args) =>
        Console.WriteLine("0 3 W".Run("LLFFFLFLFL"));

    internal static string Run(this string initial_pos, string directions) =>
        directions.ToList().Aggregate(initial_pos.Parse(), Move).Serialize();

    internal static (int, int, string) Move((int, int, string) position, char direction)
    {
        return (direction, position) switch
        {
            ('L', (int x, int y, "N")) => (x, y, "W"),
            ('L', (int x, int y, "W")) => (x, y, "S"),
            ('L', (int x, int y, "S")) => (x, y, "E"),
            ('L', (int x, int y, "E")) => (x, y, "N"),
            ('R', (int x, int y, "N")) => (x, y, "E"),
            ('R', (int x, int y, "W")) => (x, y, "N"),
            ('R', (int x, int y, "S")) => (x, y, "W"),
            ('R', (int x, int y, "E")) => (x, y, "S"),
            ('F', (int x, int y, "N")) => (x, y + 1, "N"),
            ('F', (int x, int y, "W")) => (x - 1, y, "W"),
            ('F', (int x, int y, "S")) => (x, y - 1, "S"),
            ('F', (int x, int y, "E")) => (x + 1, y, "E"),
            (_, _) => throw new NotSupportedException()
        };
    }

    public static string Serialize(this (int, int, string) pos)
        => $"{pos.Item1} {pos.Item2} {pos.Item3}";

    public static (int, int, string) Parse(this string pos)
    {
        var vals = pos.Split(" ", StringSplitOptions.RemoveEmptyEntries);
        return (int.Parse(vals[0]), int.Parse(vals[1]), vals[2]);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment