Skip to content

Instantly share code, notes, and snippets.

@opqdonut
Created December 7, 2015 15:30
Show Gist options
  • Select an option

  • Save opqdonut/2437999e48129776cbca to your computer and use it in GitHub Desktop.

Select an option

Save opqdonut/2437999e48129776cbca to your computer and use it in GitHub Desktop.
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