The Mars rover receives commands one at a time: Move, turn Left, turn Right. Moving means you move by 1 cell in the direction you are facing, and turning means changing the direction you are facing, but not moving.
Create a program to model this in order to compute the current position and heading after accepting a sequence of commands.
As starting state, you receive the position (x, y), and the facing. As output, you also receive that.
Test cases in Scala
Algorithm in Scala
34 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!
This is more of a modelling challenge: how do you best model in Scala for comprehension?
A sealed abstract class is a great way to represent a set of possibilities with their own pre-defined values. Similar to Java enum but with several differences in terms of type safety: you can limit who can extend this class by marking it as sealed. (this is © from www.scala-algorithms.com)
Another concept applied heavily is immutability, such that we never modify the existing object, only emit new ones. As such, testing becomes significantly easier and more obvious
Scala concepts & Hints
Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.
A state machine is the use of `sealed trait` to represent all the possible states (and transitions) of a 'machine' in a hierarchical form.