aboutsummaryrefslogtreecommitdiff
path: root/Puzzle8.hs
blob: 6d7514cda8ddca4afff80e532dbbdd358f5512d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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