aboutsummaryrefslogtreecommitdiff
path: root/2020/03-Toboggan_Trajectory/first.hs
blob: 55b06b1881475d8654249fec7bcc0f1f9e8aa5ca (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
-- requires cabal install --lib megaparsec
module Main (main) where

import Control.Monad (void, when)
import Data.List (foldl')
import Data.Void
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)

exampleExpectedOutput = 7

type Line = [Bool]
type Field = [Line]

type Parser = Parsec Void String

parseElt :: Parser Bool
parseElt = do
  c <- (char '#') <|> (char '.')
  return $ c == '#'

parseLine :: Parser Line
parseLine = some parseElt <* char '\n'

parseField :: Parser Field
parseField = some parseLine <* eof

parseInput :: String -> IO Field
parseInput filename = do
  input <- readFile filename
  case runParser parseField filename input of
    Left bundle -> die $ errorBundlePretty bundle
    Right field -> return field

compute :: Field -> Int
compute field = snd $ foldl' tree (0, 0) field
  where
    tree :: (Int, Int) -> Line -> (Int, Int)
    tree (index, trees) l = (next index, trees + if l !! index then 1 else 0)
    next n = (n + 3) `mod` fieldLen
    fieldLen = length $ field !! 0

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