aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-08 15:59:47 +0100
committerYuchen Pei <me@ypei.me>2017-12-08 15:59:47 +0100
commitb277242db7b2e800d666eee138ecc8aa0bda6225 (patch)
tree48642d4a6c56499d2a040183d96a5d17d364f2c2
parent667c2c410357d29c68abdd6010920a30358284b3 (diff)
changed to someone elses solution
-rw-r--r--Puzzle8.hs17
1 files changed, 17 insertions, 0 deletions
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