aboutsummaryrefslogtreecommitdiff
path: root/2020/08-Handheld_Halting/first.hs
blob: 3d011067e381c46adc43fd175c1fe517429f30f7 (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
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where
import Control.Monad (void, when)
import qualified Data.Set as S
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)

exampleExpectedOutput = 5

data Op = Nop Int | Acc Int | Jmp Int

type Parser = Parsec Void String

parseOp :: Parser Op
parseOp = do
  op <- string "nop " <|> string "acc " <|> string "jmp "
  sign <- (char '+' *> return 1) <|> (char '-' *> return (-1))
  num <- some digitChar
  void $ char '\n'
  let n = sign * (read num)
  return $ case op of
    "nop " -> Nop n
    "acc " -> Acc n
    "jmp " -> Jmp n

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 = run 0 0 S.empty
  where
    run :: Int -> Int -> S.Set Int -> Int
    run pointer acc visited
      | S.member pointer visited = acc
      | otherwise = case ops !! pointer of
          Nop _ -> run (pointer + 1) acc visited'
          Acc i -> run (pointer + 1) (acc + i) visited'
          Jmp i -> run (pointer + i) acc visited'
      where
        visited' = S.insert pointer visited

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