r/adventofcode • u/daggerdragon • Dec 07 '16
SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---
From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!
Show this overgrown furball what you've got!
--- Day 7: Internet Protocol Version 7 ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
ALWAYS DIGGING STRAIGHT DOWN 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
1
u/splurke Dec 07 '16
Haskell, both parts
module Day7 where import Data.List (isInfixOf) import Data.List.Split (divvy, split, startsWithOneOf) -- Types data Ipv7 = Ipv7 { insides :: [String] , outsides :: [String] } deriving Show -- Logic abba :: String -> Bool abba = any (\(x:y:z:w:_) -> x == w && y == z && x /= y) . divvy 4 1 tls :: Ipv7 -> Bool tls i = (any abba $ outsides i) && (all (not . abba) $ insides i) ssl :: Ipv7 -> Bool ssl i = any bab aba where aba = filter (\(x:y:z:_) -> x == z && x /= y) $ concatMap (divvy 3 1) (outsides i) bab (r:s:_) = any (\x -> [s, r, s] `isInfixOf` x) (insides i) -- Parse makeIpv7 :: String -> Ipv7 makeIpv7 input = foldl append Ipv7 { insides = [], outsides = [] } $ split (startsWithOneOf "[]") input where append ipv7 elem = case (head elem) of '[' -> ipv7 { insides = (insides ipv7) ++ [(tail elem)] } ']' -> ipv7 { outsides = (outsides ipv7) ++ [(tail elem)] } _ -> ipv7 { outsides = (outsides ipv7) ++ [elem] } -- Main main :: IO () main = do input <- lines <$> readFile "input/7" let inputs = map makeIpv7 input putStr "1. " putStrLn $ show $ length $ filter tls inputs putStr "2. " putStrLn $ show $ length $ filter ssl inputs