aboutsummaryrefslogtreecommitdiff
path: root/2020/07-Handy_Haversacks/second.hs
blob: 3d8dcda490c589b0dfc0e1cca6d0ba3548c254a2 (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
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where
import Control.Monad (void, when)
import Data.List (foldl')
import qualified Data.Map as M
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)

exampleExpectedOutput = 32
exampleExpectedOutput2 = 126

data Bag = Bag { name :: String
               , contents :: [(Int, String)]
               } deriving (Show)

type Parser = Parsec Void String

parseName :: Parser String
parseName = do
  adjective <- some letterChar
  void (char ' ')
  color <- some letterChar
  return $ adjective ++ " " ++ color

parseInt :: Parser Int
parseInt = do
  i <- some digitChar
  return $ read i

parseContent :: Parser (Int, String)
parseContent = do
  i <- parseInt
  void (char ' ')
  name <- parseName
  void (string " bag")
  void (optional $ char 's')
  void (string ", " <|> string ".")
  return (i, name)

parseContents :: Parser [(Int, String)]
parseContents = some parseContent <|> (string "no other bags." *> return [])

parseBag :: Parser Bag
parseBag = do
  name <- parseName
  void $ string " bags contain "
  contents <- parseContents
  void (char '\n')
  return $ Bag { name=name, contents=contents }

parseBags :: Parser [Bag]
parseBags = some parseBag <* eof

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

compute :: [Bag]-> Int
compute bags = (compute' "shiny gold") - 1
  where
    bagsMap :: M.Map String Bag
    bagsMap = foldl' (\acc b -> M.insert (name b) b acc) M.empty bags
    compute' :: String -> Int
    compute' s = foldl' (\acc (i, s') -> acc + i * (compute' s')) 1 (contents $ bagsMap M.! s)

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