2022-12-10 07:32:31 +01:00
import Data.List.Split
2022-12-11 12:03:12 +01:00
data OpCode
= Noop
| AddX Int
deriving ( Show , Eq )
2022-12-10 07:32:31 +01:00
main :: IO ()
main = do
2022-12-11 12:03:12 +01:00
input <- parse . lines <$> readFile " input "
let c = 1 : 1 : 1 : cycles 1 input
let vals = [ 20 , 60 , 100 , 140 , 180 , 220 ]
print $ sum [ i * c !! i | i <- vals ]
2022-12-10 07:32:31 +01:00
putStrLn $ printscr ( tail c )
parse :: [ String ] -> [ OpCode ]
parse ( l : ls )
| l == " noop " = Noop : parse ls
| head len2 == " addx " = AddX ( read $ len2 !! 1 ) : parse ls
2022-12-11 12:03:12 +01:00
where
len2 = words l
2022-12-10 07:32:31 +01:00
parse _ = []
cycles :: Int -> [ OpCode ] -> [ Int ]
cycles x ( Noop : cs ) = x : cycles x cs
2022-12-11 12:03:12 +01:00
cycles x ( AddX y : cs ) = ( x + y ) : ( x + y ) : cycles ( x + y ) cs
2022-12-10 07:32:31 +01:00
cycles _ _ = []
printscr :: [ Int ] -> String
2022-12-11 12:03:12 +01:00
printscr pxs = concat [ zipWith f ( ls !! x ) [ 0 .. ] ++ " \ n " | x <- [ 0 .. 5 ] ]
where
f x y =
if abs ( x - y ) <= 1
then '#'
else '.'
ls = chunksOf 40 pxs