From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/08a.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2020/08a.hs (limited to '2020/08a.hs') diff --git a/2020/08a.hs b/2020/08a.hs new file mode 100644 index 0000000..846c3df --- /dev/null +++ b/2020/08a.hs @@ -0,0 +1,34 @@ +module Day8A where + +import System.Environment (getArgs) + +data Instruction = Nop | Acc Int | Jmp Int + deriving (Eq, Show, Ord) + +readSigned :: String -> Int +readSigned ('+':xs) = read xs +readSigned xs = read xs + +parseInstruction :: String -> Instruction +parseInstruction xs | ins == "nop" = Nop + | ins == "acc" = Acc num + | ins == "jmp" = Jmp num + where ins = take 3 xs + num = readSigned $ drop 4 xs + +execUntilLoop :: [Instruction] -> [Int] -> Int -> Int -> Int +execUntilLoop is vs ip ax | ip `elem` vs = ax + | otherwise = case ins of Nop -> execUntilLoop is vs' (ip + 1) ax + Acc x -> execUntilLoop is vs' (ip + 1) (ax + x) + Jmp x -> execUntilLoop is vs' (ip + x) ax + where ins = is!!ip + vs' = ip : vs + +main :: IO () +main = do + args <- getArgs; + content <- readFile $ head args; + let l = lines content; + let is = map parseInstruction l; + print $ execUntilLoop is [] 0 0; + return (); \ No newline at end of file -- cgit v1.2.3