-- copypasta from https://github.com/ephemient/aoc2017/blob/master/src/Day8.hs import Data.Map.Lazy as Map exec :: (Num a, Ord a, Read a) => Map String a -> String -> Map String a exec regs line | q cond (fromMaybe 0 $ Map.lookup when regs) (read cmp) = Map.insert reg (p op (fromMaybe 0 $ Map.lookup reg regs) (read val)) regs | otherwise = regs where [reg, op, val, "if", when, cond, cmp] = words line p "inc" = (+); p "dec" = (-); q "<" = (<); q "<=" = (<=) q "==" = (==); q ">=" = (>=); q ">" = (>); q "!=" = (/=) day8a :: String -> Int day8a = maximum . Map.elems . foldl' exec Map.empty . lines day8b :: String -> Int day8b = maximum . concatMap Map.elems . scanl' exec Map.empty . lines