-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.hs
executable file
·52 lines (43 loc) · 1.72 KB
/
day6.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env stack
-- stack runghc --package parsec
import Data.Array.MArray
import Data.Ix (range)
import Data.Array.IO (IOArray)
import Control.Monad (liftM)
import Text.Parsec
import Text.Parsec.String (Parser)
import Text.Parsec.Char (char, string, digit)
countLit :: IOArray (Int, Int) Bool -> IO Int
countLit = liftM (length . filter id) . getElems
toggle :: (Ix i) => i -> i -> IOArray i Bool -> IO ()
toggle x y a = mapM_ (($ True) . writeArray a) $ range (x, y)
turnOn :: (Ix i) => i -> i -> IOArray i Bool -> IO ()
turnOn x y a = mapM_ (($ True) . writeArray a) $ range (x, y)
turnOff :: (Ix i) => i -> i -> IOArray i Bool -> IO ()
turnOff x y a = mapM_ (($ False) . writeArray a) $ range (x, y)
getFn :: (Ix i) => String -> i -> i -> IOArray i Bool -> IO ()
getFn xs
| xs == "turn on" = turnOn
| xs == "turn off" = turnOff
| xs == "toggle" = toggle
| otherwise = undefined
tuplify :: [a] -> (a,a)
tuplify [x,y] = (x,y)
how2parse :: Parser ([IOArray (Int,Int) Bool -> IO ()])
how2parse = many1 $ do
fnName <- (choice [try (string "toggle"), try (string "turn off"), try (string "turn on"), fail "Well I guess it fucked up?"])
char ' '
pair1 <- sepBy1 ((read :: String -> Int) <$> many1 digit) (char ',')
string " through "
pair2 <- sepBy1 ((read :: String -> Int) <$> many1 digit) (char ',')
optional $ char '\n'
return $ (getFn fnName) (tuplify pair1) (tuplify pair2)
modifier :: String -> String -> [IOArray (Int, Int) Bool -> IO ()]
modifier name s = either (error . show) id . (parse how2parse name) $ s
main :: IO ()
main = do
filename <- getLine
contents <- readFile filename
a <- newArray ((0, 0), (999, 999)) False
mapM_ ($ a) . modifier filename $ contents
countLit a >>= print