From c31b3c014684c8b70c75415195cd8de72644dc08 Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 11 Dec 2022 02:40:58 +0100 Subject: [PATCH] day10 --- Makefile | 2 +- day10/main.hs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 day10/main.hs diff --git a/Makefile b/Makefile index fb4fd0b..3f9abc8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -DAY = 9 +DAY = 10 DIR = day$(DAY) diff --git a/day10/main.hs b/day10/main.hs new file mode 100644 index 0000000..333093b --- /dev/null +++ b/day10/main.hs @@ -0,0 +1,64 @@ +import System.IO + +import Data.List.Split + +import Debug.Trace + + + +data Cycle = Noop | Addx1 | Addx2 Int deriving (Show) + + + +parseInstr :: String -> [Cycle] +parseInstr ('n':_) = [Noop] +parseInstr ('a':xs) = [Addx1,Addx2 dx] + where + dxs = last $ splitOn " " xs + dx = read dxs :: Int + +parseAsm :: String -> [Cycle] +parseAsm = concat . map parseInstr . lines + + + + +computeX :: Int -> Cycle -> Int +computeX x (Addx2 dx) = x + dx +computeX x _ = x + + + + +drawCRT :: [Bool] -> (Int, Int) -> [Bool] +drawCRT crt (c,x) = sx ++ [draw] ++ (tail xs) + where + (sx,xs) = splitAt c crt + sprite = [x-1,x,x+1] + col = (c `mod` 40) + draw = elem col sprite + +printCRT :: [Bool] -> String +printCRT = concat . map (++"\n") . chunksOf 40 . map crtchr + where + crtchr False = '.' + crtchr True = '#' + + + + +handler :: String -> String +handler s = (show $ sum score) ++ "\n" ++ + (printCRT crt) ++ "\n" + where + cycles = parseAsm s + xhs = scanl computeX 1 cycles + sigstr = zipWith (*) [1..] xhs + score = map ((0:sigstr) !!) [20,60..220] + crt = foldl drawCRT (replicate 240 False) (zip [0..239] xhs) + +main :: IO () +main = do + interact handler + +