AoC/2019/day14/day14.hs

27 lines
920 B
Haskell
Raw Normal View History

2019-12-14 13:12:29 +01:00
import Data.List.Split
import qualified Data.Map as M
import Data.Ratio
main :: IO ()
main = do
content <- parseContent <$> readFile "testinput1"
print content
parseContent :: String -> M.Map String [(String, Ratio Int)]
parseContent = M.fromList . map (parseRecipe . splitOn " => ") . lines
parseRecipe :: [String] -> (String, [(String,Ratio Int)])
parseRecipe r = (result,ingredients)
where parseIngredients = map (splitOn " ") . splitOn ", "
left = parseIngredients $ head r
right = concat $ parseIngredients $ last r
result = last right
resultDenom = read $ head right :: Int
processIngredient n ing = (last ing,read (head ing) % n)
ingredients = map (processIngredient resultDenom) left
part1 :: M.Map String [(String, Ratio Int)] -> (String,Ratio Int) -> Ratio Int
part1 _ ("ORE",x) = x
part1 m (key,x) = x * sum (map (part1 m) (m M.! key))