|
module Main where |
|
|
|
import Prelude (class Show, Unit, ($), (+), (-)) |
|
import Control.Monad.Eff (Eff) |
|
import Control.Monad.Eff.Console (CONSOLE, logShow) |
|
import Data.Foldable (foldlDefault) |
|
import Data.Generic.Rep (class Generic) |
|
import Data.Generic.Rep.Show (genericShow) |
|
import Data.Ord (abs) |
|
import Data.Tuple (Tuple(..), fst) |
|
|
|
data Orientation = N |
|
| S |
|
| E |
|
| W |
|
|
|
derive instance genericOrientation ∷ Generic Orientation _ |
|
|
|
instance showOrientation ∷ Show Orientation where |
|
show = genericShow |
|
|
|
data Direction = L |
|
| R |
|
|
|
derive instance genericDirection ∷ Generic Direction _ |
|
|
|
instance showDirection ∷ Show Direction where |
|
show = genericShow |
|
|
|
type Point = Tuple Int Int |
|
type Model = Tuple Point Orientation |
|
type Ins = Tuple Direction Int |
|
|
|
orientation ∷ Orientation → Direction → Orientation |
|
orientation N L = W |
|
orientation N R = E |
|
orientation S L = E |
|
orientation S R = W |
|
orientation E L = N |
|
orientation E R = S |
|
orientation W L = S |
|
orientation W R = N |
|
|
|
getMove ∷ Orientation → Direction → Point → Int → Point |
|
getMove N L (Tuple x y) z = Tuple (x - z) y |
|
getMove N R (Tuple x y) z = Tuple (x + z) y |
|
getMove S L (Tuple x y) z = Tuple (x + z) y |
|
getMove S R (Tuple x y) z = Tuple (x - z) y |
|
getMove E L (Tuple x y) z = Tuple x (y + z) |
|
getMove E R (Tuple x y) z = Tuple x (y - z) |
|
getMove W L (Tuple x y) z = Tuple x (y - z) |
|
getMove W R (Tuple x y) z = Tuple x (y + z) |
|
|
|
doMove ∷ Model → Ins → Model |
|
doMove (Tuple point ori) (Tuple dir step) = Tuple (getMove ori dir point step) (orientation ori dir) |
|
|
|
distance ∷ Point → Int |
|
distance (Tuple x y) = abs x + abs y |
|
|
|
doMoves ∷ Model → Array Ins → Point |
|
doMoves model ins = fst $ foldlDefault doMove model ins |
|
|
|
main ∷ ∀ e. Eff (console ∷ CONSOLE | e) Unit |
|
main = do |
|
let result = doMoves (Tuple (Tuple 0 0) N) testIns |
|
logShow $ distance result |
|
|
|
testIns ∷ Array Ins |
|
testIns = [(Tuple L 4), (Tuple L 3), (Tuple R 1), (Tuple L 4), |
|
(Tuple R 2), (Tuple R 2), (Tuple L 1), (Tuple L 2), |
|
(Tuple R 1), (Tuple R 1), (Tuple L 3), (Tuple R 5), |
|
(Tuple L 2), (Tuple R 5), (Tuple L 4), (Tuple L 3), |
|
(Tuple R 2), (Tuple R 2), (Tuple L 5), (Tuple L 1), |
|
(Tuple R 4), (Tuple L 1), (Tuple R 3), (Tuple L 3), |
|
(Tuple R 5), (Tuple R 2), (Tuple L 5), (Tuple R 2), |
|
(Tuple R 1), (Tuple R 1), (Tuple L 5), (Tuple R 1), |
|
(Tuple L 3), (Tuple L 2), (Tuple L 5), (Tuple R 4), |
|
(Tuple R 4), (Tuple L 2), (Tuple L 1), (Tuple L 1), |
|
(Tuple R 1), (Tuple R 1), (Tuple L 185), (Tuple R 4), |
|
(Tuple L 1), (Tuple L 1), (Tuple R 5), (Tuple R 1), |
|
(Tuple L 1), (Tuple L 3), (Tuple L 2), (Tuple L 1), |
|
(Tuple R 2), (Tuple R 2), (Tuple R 2), (Tuple L 1), |
|
(Tuple L 1), (Tuple R 4), (Tuple R 5), (Tuple R 53), |
|
(Tuple L 1), (Tuple R 1), (Tuple R 78), (Tuple R 3), |
|
(Tuple R 4), (Tuple L 1), (Tuple R 5), (Tuple L 1), |
|
(Tuple L 4), (Tuple R 3), (Tuple R 3), (Tuple L 3), |
|
(Tuple L 3), (Tuple R 191), (Tuple R 4), (Tuple R 1), |
|
(Tuple L 4), (Tuple L 1), (Tuple R 3), (Tuple L 1), |
|
(Tuple L 2), (Tuple R 3), (Tuple R 2), (Tuple R 4), |
|
(Tuple R 5), (Tuple R 5), (Tuple L 3), (Tuple L 5), |
|
(Tuple R 2), (Tuple R 3), (Tuple L 1), (Tuple L 1), |
|
(Tuple L 3), (Tuple R 1), (Tuple R 4), (Tuple R 1), |
|
(Tuple R 3), (Tuple R 4), (Tuple R 4), (Tuple R 4), |
|
(Tuple R 5), (Tuple R 2), (Tuple L 5), (Tuple R 1), |
|
(Tuple R 2), (Tuple R 5), (Tuple L 3), (Tuple L 4), |
|
(Tuple R 1), (Tuple L 5), (Tuple R 1), (Tuple L 4), |
|
(Tuple L 3), (Tuple R 5), (Tuple R 5), (Tuple L 3), |
|
(Tuple L 4), (Tuple L 4), (Tuple R 2), (Tuple R 2), |
|
(Tuple L 5), (Tuple R 3), (Tuple R 1), (Tuple R 2), |
|
(Tuple R 5), (Tuple L 5), (Tuple L 3), (Tuple R 4), |
|
(Tuple L 5), (Tuple R 5), (Tuple L 3), (Tuple R 1), |
|
(Tuple L 1), (Tuple R 4), (Tuple R 4), (Tuple L 3), |
|
(Tuple R 2), (Tuple R 5), (Tuple R 1), (Tuple R 2), |
|
(Tuple L 1), (Tuple R 4), (Tuple R 1), (Tuple L 3), |
|
(Tuple L 3), (Tuple L 5), (Tuple R 2), (Tuple R 5), |
|
(Tuple L 1), (Tuple L 4), (Tuple R 3), (Tuple R 3), |
|
(Tuple L 3), (Tuple R 2), (Tuple L 5), (Tuple R 1), |
|
(Tuple R 3), (Tuple L 3), (Tuple R 2), (Tuple L 1), |
|
(Tuple R 4), (Tuple R 3), (Tuple L 4), (Tuple R 5), |
|
(Tuple L 2), (Tuple L 2), (Tuple R 5), (Tuple R 1), |
|
(Tuple R 2), (Tuple L 4), (Tuple L 4), (Tuple L 5), |
|
(Tuple R 3), (Tuple L 4)] |