From b277242db7b2e800d666eee138ecc8aa0bda6225 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Fri, 8 Dec 2017 15:59:47 +0100 Subject: changed to someone elses solution --- Puzzle8.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Puzzle8.hs diff --git a/Puzzle8.hs b/Puzzle8.hs new file mode 100644 index 0000000..6d7514c --- /dev/null +++ b/Puzzle8.hs @@ -0,0 +1,17 @@ +-- 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 -- cgit v1.2.3