Created
November 20, 2019 11:59
-
-
Save reidev275/24ba4a5991d82ffb753448b55397172b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import Effect (Effect) | |
import Effect.Console (log) | |
data Heading = North | South | East | West | |
data Position = Position | |
{ x ∷ Int | |
, y ∷ Int | |
, heading ∷ Heading | |
} | |
instance showHeading ∷ Show Heading where | |
show North = "North" | |
show South = "South" | |
show East = "East" | |
show West = "West" | |
instance showPosition ∷ Show Position where | |
show (Position p) = "{ x:" <> (show p.x) <> ", y:" <> (show p.y) <> ", heading:" <> (show p.heading) <> " }" | |
data Dsl | |
= Pos Position | |
| Forward Dsl | |
| Right Dsl | |
rightImpl ∷ Heading → Heading | |
rightImpl North = East | |
rightImpl East = South | |
rightImpl South = West | |
rightImpl West = North | |
updateHeading ∷ Position → Position | |
updateHeading (Position p) = | |
Position p { heading = (rightImpl p.heading) } | |
forwardImpl ∷ Position → Position | |
forwardImpl (Position p) = | |
case p.heading of | |
North → Position p { y = if p.y == 9 then 0 else p.y + 1 } | |
South → Position p { y = if p.y == 0 then 9 else p.y - 1 } | |
East → Position p { x = if p.x == 9 then 0 else p.x + 1 } | |
West → Position p { x = if p.x == 0 then 9 else p.x - 1 } | |
evaluate ∷ Dsl → Position | |
evaluate (Pos p) = p | |
evaluate (Forward d) = forwardImpl $ evaluate d | |
evaluate (Right d) = updateHeading $ evaluate d | |
knight ∷ Dsl → Dsl | |
knight = Forward >>> Forward >>> Right >>> Forward | |
left ∷ Dsl → Dsl | |
left = Right >>> Right >>> Right | |
turnAround ∷ Dsl → Dsl | |
turnAround = Right >>> Right | |
backwards ∷ Dsl → Dsl | |
backwards = turnAround >>> Forward >>> turnAround | |
origin ∷ Dsl | |
origin = Pos $ Position | |
{ x: 0 | |
, y: 0 | |
, heading: North | |
} | |
result ∷ Position | |
result = evaluate $ knight origin | |
main ∷ Effect Unit | |
main = do | |
log $ show result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment