Created
April 27, 2017 19:21
-
-
Save jjst/ad989e709382105b8e1847c628bc49e1 to your computer and use it in GitHub Desktop.
katas-elm-taxicab
This file contains 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 exposing (..) | |
import String | |
import Html exposing (text) | |
type Turn | |
= L | |
| R | |
| Stay | |
type alias Instruction = | |
{ turn: Turn | |
, steps: Int | |
} | |
type Direction | |
= North | |
| South | |
| East | |
| West | |
type alias Position = | |
{ x: Int | |
, y: Int | |
, direction: Direction | |
} | |
-- yay parsing!!! | |
input = """R3, L5, R2, L2, R1, L3, R1, R3, L4, R3, L1, L1, R1, L3, R2, L3, L2, R1, R1, L1, R4, L1, L4, R3, L2, L2, R1, L1, R5, R4, R2, L5, L2, R5, R5, L2, R3, R1, R1, L3, R1, L4, L4, L190, L5, L2, R4, L5, R4, R5, L4, R1, R2, L5, R50, L2, R1, R73, R1, L2, R191, R2, L4, R1, L5, L5, R5, L3, L5, L4, R4, R5, L4, R4, R4, R5, L2, L5, R3, L4, L4, L5, R2, R2, R2, R4, L3, R4, R5, L3, R5, L2, R3, L1, R2, R2, L3, L1, R5, L3, L5, R2, R4, R1, L1, L5, R3, R2, L3, L4, L5, L1, R3, L5, L2, R2, L3, L4, L1, R1, R4, R2, R2, R4, R2, R2, L3, L3, L4, R4, L4, L4, R1, L4, L4, R1, L2, R5, R2, R3, R3, L2, L5, R3, L3, R5, L2, R3, R2, L4, L3, L1, R2, L2, L3, L5, R3, L1, L3, L4, L3""" | |
split : String -> List String | |
split text = String.split ", " text | |
turn_from_string : String -> Turn | |
turn_from_string text = case text of | |
"L" -> L | |
"R" -> R | |
_ -> Stay | |
instruction_from_string : String -> Instruction | |
instruction_from_string text = | |
let | |
turnString = String.left 1 text | |
numSteps = String.dropLeft 1 text | |
in | |
{turn = turn_from_string turnString, steps = Result.withDefault 0 (String.toInt numSteps)} | |
instructions_from_string : String -> List Instruction | |
instructions_from_string text = | |
List.map instruction_from_string (split text) | |
-- | |
make_turn : Direction -> Turn -> Direction | |
make_turn direction turn = | |
case turn of | |
L -> | |
case direction of | |
North -> West | |
West -> South | |
South -> East | |
East -> North | |
R -> | |
case direction of | |
North -> East | |
East -> South | |
South -> West | |
West -> North | |
Stay -> direction | |
make_move : Instruction -> Position -> Position | |
make_move {turn, steps} {x, y, direction} = | |
let | |
new_direction = make_turn direction turn | |
(new_x, new_y) = | |
case new_direction of | |
North -> (x, y + steps) | |
East -> (x + steps, y) | |
South -> (x, y - steps) | |
West -> (x - steps, y) | |
in | |
{x = new_x, y = new_y, direction = new_direction } | |
initialPosition = {x=0, y=0, direction=North} | |
main = | |
let | |
instructions = instructions_from_string input | |
endPosition = List.foldl make_move initialPosition instructions | |
distance = endPosition.x + endPosition.y | |
in | |
text ("distance = " ++ (toString distance)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment