Sha256: 7b8c09f18a2579e9bcca0823ff6b1e8c5ce29ed3e47e64ed9438b96339092fe0
Contents?: true
Size: 1.16 KB
Versions: 396
Compression:
Stored size: 1.16 KB
Contents
import Bearing.Bearing case class Robot(bearing: Bearing, coordinates: (Int, Int)) { val x = coordinates._1 val y = coordinates._2 def advance(): Robot = bearing match { case Bearing.North => Robot(bearing, (x, y + 1)) case Bearing.South => Robot(bearing, (x, y - 1)) case Bearing.East => Robot(bearing, (x + 1, y)) case Bearing.West => Robot(bearing, (x - 1, y)) } def turnRight(): Robot = if (bearing.id == Bearing.maxId - 1) Robot(Bearing(0), (x, y)) else Robot(Bearing(bearing.id + 1), (x, y)) def turnLeft(): Robot = if (bearing.id == 0) Robot(Bearing(Bearing.maxId - 1), (x, y)) else Robot(Bearing(bearing.id - 1), (x, y)) def simulate(instructions: String): Robot = instructions.foldLeft(this){ case (acc, instruction) => instruction match { case 'A' => acc.advance() case 'L' => acc.turnLeft() case 'R' => acc.turnRight() case _ => throw new IllegalArgumentException("Invalid instruction - " + instruction) }} } object Bearing extends Enumeration { type Bearing = Value val North = Value(0, "North") val East = Value(1, "East") val South = Value(2, "South") val West = Value(3, "West") }
Version data entries
396 entries across 396 versions & 1 rubygems