Created
December 7, 2015 15:30
-
-
Save opqdonut/2437999e48129776cbca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qualified Data.Map as M | |
| import qualified Data.Set as S | |
| import Data.List.Split | |
| import Data.List | |
| data Range = Range (Int,Int) (Int,Int) | |
| data Command = On Range | Off Range | Toggle Range | |
| type Lights = S.Set (Int,Int) | |
| inRange :: Range -> Lights | |
| inRange (Range (x0,y0) (x1,y1)) = | |
| S.fromList [(x,y) | x <- [x0..x1], y <- [y0..y1]] | |
| toggle :: Lights -> Lights -> Lights | |
| toggle l r = (l `S.difference` r) `S.union` (r `S.difference` l) | |
| apply :: Lights -> Command -> Lights | |
| apply l (On r) = l `S.union` inRange r | |
| apply l (Off r) = l `S.difference` inRange r | |
| apply l (Toggle r) = l `toggle` inRange r | |
| parse :: String -> Command | |
| parse s = p (splitOneOf " ," s) | |
| where p ["turn","on",x0,y0,"through",x1,y1] = On (r x0 y0 x1 y1) | |
| p ["turn","off",x0,y0,"through",x1,y1] = Off (r x0 y0 x1 y1) | |
| p ["toggle",x0,y0,"through",x1,y1] = Toggle (r x0 y0 x1 y1) | |
| p s = error $ "parse: " ++ show s | |
| r x0 y0 x1 y1 = Range (read x0,read y0) (read x1,read y1) | |
| main = do | |
| input <- fmap lines $ readFile "input.6" | |
| print . S.size $ foldl' apply S.empty (map parse input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment