Skip to content

Instantly share code, notes, and snippets.

@houli
Last active April 27, 2017 23:07
Show Gist options
  • Save houli/fbc3bb640b2997f3c1a81bb2edfe9a04 to your computer and use it in GitHub Desktop.
Save houli/fbc3bb640b2997f3c1a81bb2edfe9a04 to your computer and use it in GitHub Desktop.
Functional Kubs Solution
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc*
/.purs*
/.psa*

Taxicab Functional Kubs

Compile

You will need both Bower and Pulp.

  1. Create src dir
  2. Move Main.purs into src dir
  3. bower install
  4. pulp run
{
"name": "taxicab",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
],
"dependencies": {
"purescript-prelude": "^3.0.0",
"purescript-console": "^3.0.0",
"purescript-tuples": "^4.0.0",
"purescript-foldable-traversable": "^3.0.0",
"purescript-generics-rep": "^5.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
}
}
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)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment