10 lines
925 B
Haskell
10 lines
925 B
Haskell
c :: [(Int, Int)] -> [Int]
|
|
c((x,i):t)=(x*i):(c$drop 39 t);c _=[] -- This takes every 40th element of a list of tuples, and multiplies the parts
|
|
v :: (Int, Int) -> Char
|
|
v(p,c)|abs(p-(mod(c-1)40))<2='#'|let='.' -- This checks, if a cycle c 'sees' a position p.
|
|
n :: String -> String
|
|
n[]=[];n i|(f,b)<-splitAt 40 i=f++'\n':n b -- Inserts a \n every 40th position in a string
|
|
e :: [(Int, Int)] -> String -> [(Int, Int)]
|
|
e s@((x,c):t)i|(_,(_:i))<-span(/=' ')i=(x+(read i),c+2):(x,c+1):s|let=(x,c+1):s -- interprets one instruction i, evaluates it and adds the state (x,c) for each cycle c the instruction needed to the list of previous states
|
|
main :: IO()
|
|
main=interact$(\i->[n.map v,show.sum.c.drop 19]>>=($i)).reverse.foldl e[(1,1)].lines -- read from stdin, interpret (linewise) each instruction. Initial State is (1,1). On the evaluation both problems are executed, drawing (n.map v) and sum-/multiplying (show.sum.c.drop 19)
|