Tweaked task 1
[summerofcode2018soln.git] / src / task1 / task1-mpc.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3 import Data.List (foldl') -- import the strict fold
4
5 import Data.Text (Text)
6 -- import qualified Data.Text as T
7 import qualified Data.Text.IO as TIO
8
9 import Data.Void (Void)
10
11 import Text.Megaparsec -- hiding (State)
12 import Text.Megaparsec.Char
13 import qualified Text.Megaparsec.Char.Lexer as L
14 import qualified Control.Applicative as CA
15
16
17 -- number of steps
18 type Distance = Int
19
20 -- easting, northing
21 type Position = (Int, Int)
22
23 -- the directions. See below for functions for turning
24 data Direction = North | East | South | West
25 deriving (Enum, Show, Bounded, Eq)
26
27 -- The currenct state of a Mowmaster
28 data Mowmaster = Mowmaster { direction :: Direction
29 , position :: Position
30 } deriving (Show, Eq)
31
32 -- one instruction for the mowmaster
33 data Instruction = Forward Distance
34 | Clockwise
35 | Anticlockwise
36 deriving (Show, Eq)
37
38
39 main :: IO ()
40 main = do
41 instruction_text <- TIO.readFile "data/01-mowmaster.txt"
42 let instructions = successfulParse instruction_text
43 print $ part1 instructions
44 print $ part2 instructions
45
46 part1 :: [Instruction] -> Int
47 part1 = length
48
49 part2 :: [Instruction] -> Int
50 part2 = finalDistance . executeAll
51 where executeAll = foldl' execute initialMowmaster
52
53 initialMowmaster = Mowmaster East (0, 0)
54
55
56 -- Calculate manhattan distance from start to this state
57 finalDistance :: Mowmaster -> Int
58 finalDistance m = (abs e) + (abs n)
59 where (e, n) = position m
60
61
62 -- Make one move
63 execute :: Mowmaster -> Instruction -> Mowmaster
64 execute m (Forward s) = m {position = forward s (direction m) (position m)}
65 execute m Clockwise = m {direction = turnCW (direction m)}
66 execute m Anticlockwise = m {direction = turnACW (direction m)}
67
68 -- Move in the current direction
69 forward :: Distance -> Direction -> Position -> Position
70 forward s North (e, n) = (e, n+s)
71 forward s South (e, n) = (e, n-s)
72 forward s West (e, n) = (e-s, n)
73 forward s East (e, n) = (e+s, n)
74
75
76 -- | a `succ` that wraps
77 turnCW :: (Bounded a, Enum a, Eq a) => a -> a
78 turnCW dir | dir == maxBound = minBound
79 | otherwise = succ dir
80
81 -- | a `pred` that wraps
82 turnACW :: (Bounded a, Enum a, Eq a) => a -> a
83 turnACW dir | dir == minBound = maxBound
84 | otherwise = pred dir
85
86
87 -- Parse the input file
88
89 type Parser = Parsec Void Text
90
91 -- treat comment lines as whitespace
92 sc :: Parser ()
93 sc = L.space space1 lineComment CA.empty
94 where lineComment = L.skipLineComment "#"
95
96 lexeme = L.lexeme sc
97 integer = lexeme L.decimal
98 symb = L.symbol sc
99
100 -- instructions is some optional space followed by many instructions
101 instrsP = optional sc *> many instrP
102
103 -- an instruction is either F, C, or A
104 instrP = forwardP <|> cwP <|> acwP
105
106 -- parse each instruction
107 forwardP = Forward <$> (symb "F" *> integer)
108 cwP = Clockwise <$ symb "C"
109 acwP = Anticlockwise <$ symb "A"
110
111 successfulParse :: Text -> [Instruction]
112 successfulParse input =
113 case parse instrsP "input" input of
114 Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
115 Right instrs -> instrs