Advent of Code 2015 Day 6 (practice before AoC 2026 happens)
Puzzle: 2015 day 6 · Code: Day6.hs
Initial Thoughts
- I’ll want a parser for each row
- three cases: turn on, turn off, toggle
- so that’s a fairly obvious sum type
- I’ll want some way to store the grid state,
Map Coord LightStatewhereLightState = On | Offwould do the trick - Then I want some function that given the grid state and a command will return the new grid state
- Then you just fold over all the commands and you’re done
- I mean there’ll be some way to do this more efficiently but updating individual map entries should be fine for part 1.
- There’ll be some interval intersection stuff you can do for sure
- Like I think the 2D problem is literally just the 1D problem twice, so it’s not crazy-hard or anything
- I can probably do this in such a way that I don’t have to initialise the full map too
Code for Part 1
module Day6 where
import Data.Functor (($>))
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Text.Parsec as P (char, choice, digit, many1, string, try)
import Text.ParserCombinators.Parsec (Parser, parse)
type Coord = (Int, Int)
data LightState = On | Off deriving (Show, Eq)
type GridState = M.Map Coord LightState
data Command
= Turn LightState Coord Coord
| Toggle Coord Coord
deriving (Show)
part1 :: IO Int
part1 = M.size . M.filter (== On) . foldl' applyCommand (M.empty) <$> getInput
applyCommand :: GridState -> Command -> GridState
applyCommand gs (Turn l from to) = foldl' (setLight l) gs (coordsFromRange from to)
applyCommand gs (Toggle from to) = foldl' (toggleLight) gs (coordsFromRange from to)
toggleLight :: GridState -> Coord -> GridState
toggleLight gs c = M.alter (Just . flipLight . fromMaybe Off) c gs
where
flipLight On = Off
flipLight Off = On
setLight :: LightState -> GridState -> Coord -> GridState
setLight ls gs c = M.insert c ls gs
coordsFromRange :: Coord -> Coord -> [Coord]
coordsFromRange (x1, y1) (x2, y2) = [(x, y) | x <- [x1 .. x2], y <- [y1 .. y2]]
-- this parser is ugly but works, will tidy up later
commandParser :: Parser Command
commandParser = P.choice . map P.try $ [toggleParser, turnParser]
where
-- parses strings like "toggle x1,y1 through x2,y2"
toggleParser :: Parser Command
toggleParser = Toggle <$> (P.string "toggle " *> coordsParser <* P.string " through ") <*> coordsParser
-- parses turn <on|off> x1,y1 through x2,y2"
turnParser :: Parser Command
turnParser = Turn <$> (P.string "turn " *> lightStateParser <* P.char ' ') <*> (coordsParser <* P.string " through ") <*> coordsParser
lightStateParser :: Parser LightState
lightStateParser =
P.choice . map P.try $
[ P.string "on" $> On,
P.string "off" $> Off
]
-- there's be some way to simplify this for sure
coordsParser = (\x -> \y -> (x, y)) <$> intParser <* (P.char ',') <*> intParser
intParser = read <$> P.many1 (P.digit)
getInput :: IO [Command]
getInput = map (unsafeParse commandParser) . lines <$> readFile "./fixtures/input6.txt"
unsafeParse :: Parser a -> String -> a
unsafeParse p s = case parse p "" s of
Left res -> error . show $ res
Right res -> res
Reflections on Part 1
-
my parser was a bit ugly for part 1 but it works at least!
- it’s quite nice how quickly I could write it, but I would have been a bit stuck if I didn’t have a hoard of previous examples of mine
- plus I made a silly mistake where I forgot to give my data types
Showinstances so I was like “why the heck is nothing printing for a while!!!”
-
this code was really quite slow, it took like 11 seconds
-
I made a really really silly mistake where my
flipLightfunction was defined likeflipLight On = Off flipLight Off = Offwhich took me ages to debug lol
Initial Thoughts on Part 2
- this isn’t really so much of a scale increase thankfully, they’ve just changed the meaning of what a command is
- it should actually be very easy to adapt my existing solution for this change
- the map should now store ints rather than just On/Off and I think the changes to
toggleLight+setLightshould be very straightforward - I can recycle the same commandParser and just write a function
Command -> BrightnessChangeor something - Means I’ll probably adjust both files at the end, but that’s fine!
- Oh and the brightness is zero or more, so I’ll need to ensure that the brightnesses never go below zero. Again that’s easy enough
Code for Part 2
Here are the only parts that are new.
data BrightnessChange = BC Coord Coord Int
deriving (Show)
type BrightnessGridState = M.Map Coord Int
part2 :: IO Int
part2 = sum . M.elems . foldl' applyBrightnessChange (M.empty) . map commandToBrightnessChange <$> getInput
applyBrightnessChange :: BrightnessGridState -> BrightnessChange -> BrightnessGridState
applyBrightnessChange gs (BC from to delta) = foldl' (changeBrightness) gs (coordsFromRange from to)
where
changeBrightness :: BrightnessGridState -> Coord -> BrightnessGridState
changeBrightness gs' c = M.alter (Just . (\x -> max 0 (x + delta)) . fromMaybe 0) c gs'
commandToBrightnessChange :: Command -> BrightnessChange
commandToBrightnessChange (Toggle from to) = BC from to 2
commandToBrightnessChange (Turn Off from to) = BC from to (-1)
commandToBrightnessChange (Turn On from to) = BC from to 1
Final Thoughts
- Hoogle is still very useful to me, because I couldn’t remember the name of the function that does this
Map k v -> [v]but Hoogle knows - I’m glad I can still write parsers but oh boy was mine ugly, I’ve tried to simplify it a little at least (see below)
- Performance was pretty poor for both parts, I think naively updating a mega-map is not the intended approach
- If this was the real event I’d likely be glued to the solutions thread
(\x -> \y -> (x, y))is just(,). I swear I used to have a much angrier linter in the past that would tell me this sort of thing…- I initially had a data type like
data CoordRange = CR Coord Coordand re-introducing it makes the parser a bit nicer and I think makes some of the passing around of function args slightly less tedious - Using the magical settings of
:set -fobject-code -O2makes part 2 about twice as fast for me - In fact I kind of wonder if I should put some effort into running these solutions “properly” because I always just run them from inside
stack ghci… - I still think big performance gains would come from
- using some sort of interval approach
- keeping my approach but using something better than
Maplike aUArrayor something
Final Full Solution
module Day6 where
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Text.Parsec as P (char, choice, digit, many1, string, try)
import Text.ParserCombinators.Parsec (Parser, parse)
type Coord = (Int, Int)
data CoordRange = CR Coord Coord deriving (Show)
data LightState = On | Off deriving (Show, Eq)
type GridState = M.Map Coord LightState
data Command
= Turn LightState CoordRange
| Toggle CoordRange
deriving (Show)
data BrightnessChange = BC CoordRange Int
deriving (Show)
type BrightnessGridState = M.Map Coord Int
part1 :: IO Int
part1 = M.size . M.filter (== On) . foldl' applyCommand (M.empty) <$> getInput
part2 :: IO Int
part2 = sum . foldl' applyBrightnessChange (M.empty) . map commandToBrightnessChange <$> getInput
applyBrightnessChange :: BrightnessGridState -> BrightnessChange -> BrightnessGridState
applyBrightnessChange gs (BC cr delta) = foldl' (changeBrightness) gs (coordsFromRange cr)
where
changeBrightness :: BrightnessGridState -> Coord -> BrightnessGridState
changeBrightness gs' c = M.alter (Just . (\x -> max 0 (x + delta)) . fromMaybe 0) c gs'
commandToBrightnessChange :: Command -> BrightnessChange
commandToBrightnessChange (Toggle cr) = BC cr 2
commandToBrightnessChange (Turn Off cr) = BC cr (-1)
commandToBrightnessChange (Turn On cr) = BC cr 1
applyCommand :: GridState -> Command -> GridState
applyCommand gs (Turn l cr) = foldl' (setLight l) gs (coordsFromRange cr)
applyCommand gs (Toggle cr) = foldl' (toggleLight) gs (coordsFromRange cr)
toggleLight :: GridState -> Coord -> GridState
toggleLight gs c = M.alter (Just . flipLight . fromMaybe Off) c gs
where
flipLight On = Off
flipLight Off = On
setLight :: LightState -> GridState -> Coord -> GridState
setLight ls gs c = M.insert c ls gs
coordsFromRange :: CoordRange -> [Coord]
coordsFromRange (CR (x1, y1) (x2, y2)) = [(x, y) | x <- [x1 .. x2], y <- [y1 .. y2]]
commandParser :: Parser Command
commandParser = P.choice . map P.try $ [toggleParser, turnParser]
where
-- parses strings like "toggle x1,y1 through x2,y2"
toggleParser = Toggle <$ P.string "toggle " <*> coordRangeParser
-- parses strings like "turn <on|off> x1,y1 through x2,y2"
turnParser = Turn <$ P.string "turn " <*> lightStateParser <* P.char ' ' <*> coordRangeParser
lightStateParser = P.choice . map P.try $ [On <$ P.string "on", Off <$ P.string "off"]
coordRangeParser = CR <$> coordsParser <* P.string " through " <*> coordsParser
coordsParser = (,) <$> intParser <* P.char ',' <*> intParser
intParser = read <$> P.many1 P.digit
getInput :: IO [Command]
getInput = map (unsafeParse commandParser) . lines <$> readFile "./fixtures/input6.txt"
unsafeParse :: Parser a -> String -> a
unsafeParse p s = case parse p "" s of
Left res -> error . show $ res
Right res -> res