-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12a.hs
72 lines (56 loc) · 1.73 KB
/
12a.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-# LANGUAGE OverloadedStrings #-}
import Data.Either
import Data.List
import Text.Parsec hiding (count)
import Prelude
type Coordinate = (Int, Int)
type Heading = Int
type Position = (Coordinate, Heading)
data Instruction
= N Int
| W Int
| S Int
| E Int
| L Int
| R Int
| F Int
deriving (Show, Eq)
main = do
input <- getContents
putStr $ show $ fn $ lines input
dataIntP :: String -> (Int -> a) -> Parsec String () a
dataIntP x t = do
try $ string x
t . read <$> many1 digit
instructionP :: Parsec String () Instruction
instructionP =
dataIntP "N" N
<|> dataIntP "W" W
<|> dataIntP "S" S
<|> dataIntP "E" E
<|> dataIntP "L" L
<|> dataIntP "R" R
<|> dataIntP "F" F
plusX :: Int -> Position -> Position
plusX z ((x, y), heading) = ((x + z, y), heading)
plusY :: Int -> Position -> Position
plusY z ((x, y), heading) = ((x, y + z), heading)
plusH :: Int -> Position -> Position
plusH z ((x, y), heading) = ((x, y), (heading + z) `mod` 360)
forward :: Int -> Position -> Position
forward z (c, 0) = plusY (- z) (c, 0)
forward z (c, 180) = plusY z (c, 180)
forward z (c, 90) = plusX z (c, 90)
forward z (c, 270) = plusX (- z) (c, 270)
getNextCoordinate :: Position -> Instruction -> Position
getNextCoordinate p (N z) = plusY (- z) p
getNextCoordinate p (S z) = plusY z p
getNextCoordinate p (W z) = plusX (- z) p
getNextCoordinate p (E z) = plusX z p
getNextCoordinate p (L z) = plusH (- z) p
getNextCoordinate p (R z) = plusH z p
getNextCoordinate p (F z) = forward z p
startPosition = ((0, 0), 90)
getMannhattanDistance :: Coordinate -> Int
getMannhattanDistance (x, y) = abs $ x + y
fn xs = getMannhattanDistance . fst $ foldl' getNextCoordinate startPosition $ rights $ map (runParser instructionP () "") xs