stanosphere

Advent of Code 2015 Day 6 (practice before AoC 2026 happens)

Puzzle: 2015 day 6 · Code: Day6.hs

Initial Thoughts

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

Initial Thoughts on Part 2

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

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