-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc10.hs
26 lines (24 loc) · 831 Bytes
/
aoc10.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Control.Monad
import Data.Foldable (Foldable(foldl'))
import System.Environment (getArgs)
doline :: String -> [Int] -> [Int]
doline "noop" lst = head lst : lst
doline ('a':'d':'d':'x':s) lst = (read s + head lst) : head lst : lst
doline g _ = error $ "Bad cmd " ++ g
main :: IO ()
main = do
args <- getArgs
let filename =
if null args
then "aoc10.in"
else head args
s <- lines <$> readFile filename
let xvals = reverse $ foldl' (flip doline) [1] s
print $ sum $ map (\idx -> idx * (xvals !! (idx - 1))) [20, 60, 100, 140, 180, 220]
putStrLn ""
forM_ [0 .. 239] $ \idx -> do
let col = idx `mod` 40
if abs ((xvals !! idx) - col) <= 1
then putChar '\9608'
else putChar '.'
when (col == 39) (putChar '\n')