From 218b09f2e97e7dbbf9b955b411002fc8f878872c Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Fri, 8 Dec 2017 00:34:34 -0800 Subject: [PATCH] Day 8, part 2 --- 8.hs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/8.hs b/8.hs index 7a318ec..9f894aa 100644 --- a/8.hs +++ b/8.hs @@ -1,6 +1,5 @@ -import Data.HashMap (Map, insert, alter, empty, toList) +import Data.HashMap (Map, insert, alter, empty, toList, findWithDefault) import qualified Data.HashMap as M (lookup) -import Debug.Trace data Instruction = I { reg :: String, @@ -40,17 +39,19 @@ parseLine s = fnc = flip (getFnc function) $ read argument } -executeInstruction :: Map String Int -> Instruction -> Map String Int -executeInstruction m (I r i s f) = +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 in if f value - then alter (addToMaybe i) r newMap - else newMap + then (alter (addToMaybe i) r newMap, newHighest) + else (newMap, highest) main :: IO () main = do input <- readFile "8.txt" let instructions = map parseLine $ lines input - finalMap = foldl executeInstruction empty instructions + (finalMap, highest) = foldl executeInstruction (empty, 0) instructions maxValue = maximum . snd . unzip . toList $ finalMap - print $ maxValue \ No newline at end of file + print $ maxValue + print $ highest \ No newline at end of file