1
0
Fork 0

Day 8 - turns out getOrSetZero is redundant

This commit is contained in:
Jonathan Chan 2017-12-12 09:29:03 -08:00
parent 218b09f2e9
commit d3506becf4
1 changed files with 10 additions and 17 deletions

25
8.hs
View File

@ -1,9 +1,8 @@
import Data.HashMap (Map, insert, alter, empty, toList, findWithDefault)
import qualified Data.HashMap as M (lookup)
import Data.HashMap (Map, alter, empty, toList, findWithDefault)
data Instruction = I {
reg :: String,
inc :: Int,
val :: Int,
src :: String,
fnc :: Int -> Bool
}
@ -17,22 +16,16 @@ getFnc s = case s of
"==" -> (==)
"!=" -> (/=)
getOrSetZero :: String -> Map String Int -> (Int, Map String Int)
getOrSetZero s m =
case M.lookup s m of
Just value -> (value, m)
Nothing -> (0, insert s 0 m)
addToMaybe :: Int -> Maybe Int -> Maybe Int
addToMaybe i (Just x) = Just $ i + x
addToMaybe i Nothing = Just i
parseLine :: String -> Instruction
parseLine s =
let (register : command : value : _ : source : function : argument : _) = words s
let (register : operation : value : _ : source : function : argument : _) = words s
in I {
reg = register,
inc = case command of
val = case operation of
"inc" -> read value
"dec" -> (- read value),
src = source,
@ -40,12 +33,12 @@ parseLine s =
}
executeInstruction :: (Map String Int, Int) -> Instruction -> (Map String Int, Int)
executeInstruction (m, highest) (I r i s f) =
let (value, newMap) = getOrSetZero s m
newHighest = max highest $ findWithDefault highest r newMap
executeInstruction (m, highest) (I r v s f) =
let value = findWithDefault 0 s m
newHighest = max highest $ findWithDefault highest r m
in if f value
then (alter (addToMaybe i) r newMap, newHighest)
else (newMap, highest)
then (alter (addToMaybe v) r m, newHighest)
else (m, highest)
main :: IO ()
main = do