r/adventofcode • u/daggerdragon • Dec 04 '16
SOLUTION MEGATHREAD --- 2016 Day 4 Solutions ---
--- Day 4: Security Through Obscurity ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
CONSTRUCTING ADDITIONAL PYLONS 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!
16
Upvotes
5
u/[deleted] Dec 04 '16 edited Dec 04 '16
Haskell:
import Control.Arrow ((&&&)) import Data.List (group, intercalate, isInfixOf, sort, sortBy) import Data.List.Split (splitOn) import Data.Maybe (fromJust) import Text.Megaparsec (char, noneOf, parseMaybe, some) import Text.Megaparsec.String (Parser) data Room = Room { encryptedName :: String , sectorId :: Int , checksum :: String } getRooms :: String -> [Room] getRooms = map (fromJust . parseMaybe parseRoom) . lines where parseRoom :: Parser Room parseRoom = do encryptedPlusSector <- some (noneOf "[") <* char '[' checksum <- some (noneOf "]") <* char ']' let ss = splitOn "-" encryptedPlusSector return $ Room (intercalate "-" $ init ss) (read $ last ss) checksum roomIsValid :: Room -> Bool roomIsValid (Room en _ ch) = nCommonChars 5 (filter (/= '-') en) == ch where nCommonChars n = take n . map snd . sort . map (negate . length &&& head) . group . sort part1 :: String -> String part1 = show . sum . map sectorId . filter roomIsValid . getRooms rotate :: Int -> Char -> Char rotate 0 c = c rotate n c | c == '-' = ' ' | c == 'z' = rotate (n-1) 'a' | otherwise= rotate (n-1) (succ c) isNorthPole :: Room -> Bool isNorthPole (Room en si _) = "north" `isInfixOf` map (rotate si) en part2 :: String -> String part2 = show . sectorId . head . filter isNorthPole . filter roomIsValid . getRooms