Day 8, part 2

This commit is contained in:
Jonathan Chan 2017-12-08 00:34:34 -08:00
parent 8aa2be284b
commit 218b09f2e9
1 changed files with 9 additions and 8 deletions

17
8.hs
View File

@ -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 qualified Data.HashMap as M (lookup)
import Debug.Trace
data Instruction = I { data Instruction = I {
reg :: String, reg :: String,
@ -40,17 +39,19 @@ parseLine s =
fnc = flip (getFnc function) $ read argument fnc = flip (getFnc function) $ read argument
} }
executeInstruction :: Map String Int -> Instruction -> Map String Int executeInstruction :: (Map String Int, Int) -> Instruction -> (Map String Int, Int)
executeInstruction m (I r i s f) = executeInstruction (m, highest) (I r i s f) =
let (value, newMap) = getOrSetZero s m let (value, newMap) = getOrSetZero s m
newHighest = max highest $ findWithDefault highest r newMap
in if f value in if f value
then alter (addToMaybe i) r newMap then (alter (addToMaybe i) r newMap, newHighest)
else newMap else (newMap, highest)
main :: IO () main :: IO ()
main = do main = do
input <- readFile "8.txt" input <- readFile "8.txt"
let instructions = map parseLine $ lines input let instructions = map parseLine $ lines input
finalMap = foldl executeInstruction empty instructions (finalMap, highest) = foldl executeInstruction (empty, 0) instructions
maxValue = maximum . snd . unzip . toList $ finalMap maxValue = maximum . snd . unzip . toList $ finalMap
print $ maxValue print $ maxValue
print $ highest