aboutsummaryrefslogtreecommitdiff
path: root/2020/12-Rain_Risk/first.hs
blob: e4e204e3efa1a3b8aad76cebead76387d7daa185 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where
import Control.Monad (void, when)
import Data.List (foldl')
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)

exampleExpectedOutput = 25

data Action = N | S | E | W | L | R | F
data Heading = HN | HS | HE | HW
data Op = Op Action Int

type Parser = Parsec Void String

parseOp :: Parser Op
parseOp = do
  action <- (char 'N' *> return N)
        <|> (char 'S' *> return S)
        <|> (char 'E' *> return E)
        <|> (char 'W' *> return W)
        <|> (char 'L' *> return L)
        <|> (char 'R' *> return R)
        <|> (char 'F' *> return F)
  num <- some digitChar
  void $ char '\n'
  return $ Op action (read num)

parseOps :: Parser [Op]
parseOps = some parseOp <* eof

parseInput :: String -> IO [Op]
parseInput filename = do
  input <- readFile filename
  case runParser parseOps filename input of
    Left bundle -> die $ errorBundlePretty bundle
    Right ops -> return ops

compute :: [Op]-> Int
compute ops = (\(x, y, _) -> abs(x)+abs(y)) $ foldl' step (0, 0, HE) ops
  where
    step :: (Int, Int, Heading) -> Op -> (Int, Int, Heading)
    step (x, y, h) (Op N i) = (x, y+i, h)
    step (x, y, h) (Op S i) = (x, y-i, h)
    step (x, y, h) (Op E i) = (x+i, y, h)
    step (x, y, h) (Op W i) = (x-i, y, h)
    step (x, y, HN) (Op F i) = (x, y+i, HN)
    step (x, y, HS) (Op F i) = (x, y-i, HS)
    step (x, y, HE) (Op F i) = (x+i, y, HE)
    step (x, y, HW) (Op F i) = (x-i, y, HW)
    step (x, y, h) (Op a i) = (x, y, turn h a i)
    turn :: Heading -> Action -> Int -> Heading
    turn h a i = turn' h a mi
      where mi = (i `mod` 360) `div` 90
    turn' HN L 1 = HW
    turn' HN L 2 = HS
    turn' HN L 3 = HE
    turn' HS L 1 = HE
    turn' HS L 2 = HN
    turn' HS L 3 = HW
    turn' HE L 1 = HN
    turn' HE L 2 = HW
    turn' HE L 3 = HS
    turn' HW L 1 = HS
    turn' HW L 2 = HE
    turn' HW L 3 = HN
    turn' HN R 1 = HE
    turn' HN R 2 = HS
    turn' HN R 3 = HW
    turn' HS R 1 = HW
    turn' HS R 2 = HN
    turn' HS R 3 = HE
    turn' HE R 1 = HS
    turn' HE R 2 = HW
    turn' HE R 3 = HN
    turn' HW R 1 = HN
    turn' HW R 2 = HE
    turn' HW R 3 = HS
    turn' h _ _ = h

main :: IO ()
main = do
  example <- parseInput "example"
  let exampleOutput = compute example
  when  (exampleOutput /= exampleExpectedOutput)  (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
  input <- parseInput "input"
  print $ compute input