AoC2018/Day7/Part1.hs

26 lines
796 B
Haskell
Executable File

#! /usr/bin/env stack
-- stack --resolver lts-12.20 --install-ghc script
{-# LANGUAGE ViewPatterns #-}
module Main where
import Data.Function (fix)
import qualified Data.List as L
import qualified Data.Set as S
import qualified Data.Map as M
main :: IO ()
main = interact $ (. (M.unionsWith (<>) . map parseLine . lines)) $ fix $ \repeat depSets ->
if M.null depSets then "" else let
(noDep, _) = M.findMin $ M.filter S.null depSets
depSets' = M.map (S.delete noDep) (M.delete noDep depSets)
in noDep : repeat depSets'
parseLine :: String -> M.Map Char (S.Set Char)
parseLine (words -> ws) =
M.unionWith (<>) (M.singleton req S.empty)
(M.singleton step (S.singleton req))
where
req = (head $ ws !! 1)
step = (head $ ws !! 7)