r/adventofcode • • Dec 08 '16

SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---

#AoC_Ops:

[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION

We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg


--- Day 8: Two-Factor Authentication ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


:(){ :|:& };: IS MANDATORY [?]

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

10 Upvotes

197 comments sorted by

View all comments

Show parent comment

3

u/Tarmen Dec 08 '16 edited Dec 08 '16

I went with a simple parsec parser which bloated the solution somewhat. Also basic OCR so you don't have to actually read from the screen. I created the masks with a recursive vim macro so it only works for letters that were in my solution, though.

import Data.List
import Data.List.Split
import Text.Parsec.String
import Text.Parsec

data Command = Col Int Int | Row Int Int | Square Int Int deriving Show
type Field = [[Bool]]

main = do parsed <- parseFromFile parseAll "in08.txt"
          let result = process <$> parsed
          case result of
            Left err -> print err
            Right matrix ->  do
              let drawn = drawMatrix matrix
              print . countLights $ matrix
              print . ocr $ drawn
              mapM_ print $ drawn

countLights = length . filter id . concat

start = replicate 6 $ replicate 50 False

process :: [Command] -> Field
process = foldl' (flip step) start
  where
    step (Col amount col) = rotateCol col amount
    step (Row amount row) = rotateRow row amount
    step (Square x y)     = draw y x

rotateRow amount row = replaceBy row (rotateBy amount)
rotateCol amount col = transpose . rotateRow amount col . transpose

rotateBy i ls = recombine $ splitAt  (length ls - i) ls
  where recombine = uncurry . flip $ (++)

replaceBy ::  Int -> (a -> a) -> [a] -> [a]
replaceBy i f ls = start ++ [selected'] ++ end
  where (start, (selected:end)) = splitAt i ls
        selected' = f selected

draw x y matrix = do (i, row) <- zip [0..] matrix
                     return $do
                       (j, entry) <- zip [0..] row
                       return $ (i < x) && (j < y)  || entry

--------------------------------------------------------------------------------

slices i = map concat . transpose . map (chunksOf i)
ocr = traverse translateLetter . slices 5
  where 
    translateLetter = flip lookup samples
    samples = [(".##..#..#.#....#....#..#..##..", 'C'),
               ("####.#....###..#....#....#....", 'F'),
               ("#....#....#....#....#....####.", 'L'), 
               ("####.#....###..#....#....####.", 'E'),
               (".##..#..#.#..#.#..#.#..#..##..", 'O'),
               ("#...##...#.#.#...#....#....#..", 'Y'),
               (".###.#....#.....##.....#.###..", 'S')]


drawMatrix =  map drawLine
  where drawLine = map drawChar
        drawChar c = if c then '#' else '.'

parseAll :: Parser [Command]
parseAll = sepEndBy parseCommand spaces
parseCommand = try parseCol <|> try parseRow <|> parseSquare <?> "Command"

parseCol = uncurry Col <$> parseSingle "rotate column x=" "by"  
parseRow = uncurry Row <$> parseSingle "rotate row y=" "by"  
parseSquare = uncurry Square <$> parseSingle "rect" "x"  

parseSingle start sep = do string start >> spaces
                           l <- number
                           spaces *> string sep <* spaces
                           r <- number
                           return $ (l, r)

number :: Read a => Num a => Parser a
number = read <$> many1 digit <* spaces

1

u/haoformayor Dec 09 '16

ah, yeah, the curse of parser combinators: wonderful but a little on the verbose side. still, though, a good solution. plus you never know when that parser combinator know-how is going to come in handy.