From 1078b40201fac6b85f00f185a11d88f01a2bfadd Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 12 Dec 2017 09:29:08 -0800 Subject: [PATCH] Day 8 - Used scanl, made things more concise --- 8.hs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/8.hs b/8.hs index 616ba22..d140908 100644 --- a/8.hs +++ b/8.hs @@ -1,4 +1,4 @@ -import Data.HashMap (Map, alter, empty, toList, findWithDefault) +import Data.HashMap (Map, alter, empty, findWithDefault, elems) data Instruction = I { reg :: String, @@ -32,19 +32,13 @@ parseLine s = fnc = flip (getFnc function) $ read argument } -executeInstruction :: (Map String Int, Int) -> Instruction -> (Map String Int, Int) -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 v) r m, newHighest) - else (m, highest) +executeInstruction :: Map String Int -> Instruction -> Map String Int +executeInstruction m (I r v s f) = + alter (addToMaybe . (*v) . fromEnum . f $ findWithDefault 0 s m) r m main :: IO () main = do input <- readFile "8.txt" - let instructions = map parseLine $ lines input - (finalMap, highest) = foldl executeInstruction (empty, 0) instructions - maxValue = maximum . snd . unzip . toList $ finalMap - print $ maxValue - print $ highest \ No newline at end of file + let maxima = map (maximum . elems) . tail . scanl executeInstruction empty . map parseLine . lines $ input + print $ last maxima + print $ maximum maxima \ No newline at end of file