1
0
Fork 0

Day 8 - Used scanl, made things more concise

This commit is contained in:
Jonathan Chan 2017-12-12 09:29:08 -08:00
parent a5e12ca387
commit 1078b40201
1 changed files with 7 additions and 13 deletions

20
8.hs
View File

@ -1,4 +1,4 @@
import Data.HashMap (Map, alter, empty, toList, findWithDefault) import Data.HashMap (Map, alter, empty, findWithDefault, elems)
data Instruction = I { data Instruction = I {
reg :: String, reg :: String,
@ -32,19 +32,13 @@ parseLine s =
fnc = flip (getFnc function) $ read argument fnc = flip (getFnc function) $ read argument
} }
executeInstruction :: (Map String Int, Int) -> Instruction -> (Map String Int, Int) executeInstruction :: Map String Int -> Instruction -> Map String Int
executeInstruction (m, highest) (I r v s f) = executeInstruction m (I r v s f) =
let value = findWithDefault 0 s m alter (addToMaybe . (*v) . fromEnum . f $ findWithDefault 0 s m) r m
newHighest = max highest $ findWithDefault highest r m
in if f value
then (alter (addToMaybe v) r m, newHighest)
else (m, highest)
main :: IO () main :: IO ()
main = do main = do
input <- readFile "8.txt" input <- readFile "8.txt"
let instructions = map parseLine $ lines input let maxima = map (maximum . elems) . tail . scanl executeInstruction empty . map parseLine . lines $ input
(finalMap, highest) = foldl executeInstruction (empty, 0) instructions print $ last maxima
maxValue = maximum . snd . unzip . toList $ finalMap print $ maximum maxima
print $ maxValue
print $ highest