Day 8 - turns out getOrSetZero is redundant
This commit is contained in:
parent
218b09f2e9
commit
d3506becf4
27
8.hs
27
8.hs
|
@ -1,9 +1,8 @@
|
||||||
import Data.HashMap (Map, insert, alter, empty, toList, findWithDefault)
|
import Data.HashMap (Map, alter, empty, toList, findWithDefault)
|
||||||
import qualified Data.HashMap as M (lookup)
|
|
||||||
|
|
||||||
data Instruction = I {
|
data Instruction = I {
|
||||||
reg :: String,
|
reg :: String,
|
||||||
inc :: Int,
|
val :: Int,
|
||||||
src :: String,
|
src :: String,
|
||||||
fnc :: Int -> Bool
|
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 :: Int -> Maybe Int -> Maybe Int
|
||||||
addToMaybe i (Just x) = Just $ i + x
|
addToMaybe i (Just x) = Just $ i + x
|
||||||
addToMaybe i Nothing = Just i
|
addToMaybe i Nothing = Just i
|
||||||
|
|
||||||
parseLine :: String -> Instruction
|
parseLine :: String -> Instruction
|
||||||
parseLine s =
|
parseLine s =
|
||||||
let (register : command : value : _ : source : function : argument : _) = words s
|
let (register : operation : value : _ : source : function : argument : _) = words s
|
||||||
in I {
|
in I {
|
||||||
reg = register,
|
reg = register,
|
||||||
inc = case command of
|
val = case operation of
|
||||||
"inc" -> read value
|
"inc" -> read value
|
||||||
"dec" -> (- read value),
|
"dec" -> (- read value),
|
||||||
src = source,
|
src = source,
|
||||||
|
@ -40,12 +33,12 @@ parseLine s =
|
||||||
}
|
}
|
||||||
|
|
||||||
executeInstruction :: (Map String Int, Int) -> Instruction -> (Map String Int, Int)
|
executeInstruction :: (Map String Int, Int) -> Instruction -> (Map String Int, Int)
|
||||||
executeInstruction (m, highest) (I r i s f) =
|
executeInstruction (m, highest) (I r v s f) =
|
||||||
let (value, newMap) = getOrSetZero s m
|
let value = findWithDefault 0 s m
|
||||||
newHighest = max highest $ findWithDefault highest r newMap
|
newHighest = max highest $ findWithDefault highest r m
|
||||||
in if f value
|
in if f value
|
||||||
then (alter (addToMaybe i) r newMap, newHighest)
|
then (alter (addToMaybe v) r m, newHighest)
|
||||||
else (newMap, highest)
|
else (m, highest)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
|
Loading…
Reference in New Issue