From 06b14ed84c0a8ec11462f0a6f7397c3e3a52654d Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 8 Dec 2020 12:33:22 +0530 Subject: add initial day08 solution --- execs/Day08.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 execs/Day08.hs (limited to 'execs') diff --git a/execs/Day08.hs b/execs/Day08.hs new file mode 100644 index 0000000..7d3fce8 --- /dev/null +++ b/execs/Day08.hs @@ -0,0 +1,45 @@ +module Main where + +import Data.Set (Set) +import qualified Data.Set as Set +import Data.Map (Map) +import qualified Data.Map as Map +import Utils + +type Op = (String, Int) + +parseLine :: [String] -> Op +parseLine [s, j] = (s, read (dropWhile (== '+') j)) + +run :: Int -> Int -> Set Int -> Map Int Op -> Int +run acc pc visited operations = if Set.member pc visited then acc else handleCase + where visited' = Set.insert pc visited + handleCase = + case Map.lookup pc operations of + Just ("acc", v) -> run (acc + v) (pc + 1) visited' operations + Just ("nop", _) -> run acc (pc + 1) visited' operations + Just ("jmp", j) -> run acc (pc + j) visited' operations + _ -> acc + +doesEnd :: Int -> Int -> Set Int -> Map Int Op -> Bool +doesEnd acc pc visited operations = not (Set.member pc visited) && handleCase + where visited' = Set.insert pc visited + handleCase = + case Map.lookup pc operations of + Just ("acc", v) -> doesEnd (acc + v) (pc + 1) visited' operations + Just ("nop", _) -> doesEnd acc (pc + 1) visited' operations + Just ("jmp", j) -> doesEnd acc (pc + j) visited' operations + _ -> True -- pc has crossed the end! + +genAll :: [Op] -> [[Op]] +genAll [] = [] +genAll (n@("nop",v):rest) = (("jmp",v):rest) : map (n:) (genAll rest) +genAll (j@("jmp",v):rest) = (("nop",v):rest) : map (j:) (genAll rest) +genAll (acc:rest) = map (acc:) $ genAll rest + +main :: IO () +main = do + n <- map (parseLine . words) . lines <$> readFile "input/08" + let solve1 = run 0 0 Set.empty . Map.fromList . zip [0..] + print $ solve1 n + print $ solve1 $ head $ filter (doesEnd 0 0 Set.empty . Map.fromList . zip [0..]) $ genAll n -- cgit v1.2.3