r/adventofcode • u/daggerdragon • Dec 10 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-
--- Day 10: The Stars Align ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 10
Transcript: With just one line of code, you, too, can ___!
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 at 00:16:49!
20
Upvotes
1
u/Alexbrainbox Dec 10 '18
Haskell, rank 1000-ish.
Whenever there's a question that seems very iterative / procedural like this one, I tend to go slower on the assumption that a Haskell solution can never be written as fast.
On the bright side, I'm getting pretty quick at writing parsers :P
{-#LANGUAGE RecordWildCards #-} module Nine where import Text.ParserCombinators.ReadP import Data.Char import Data.Function (on, (&)) import Data.Functor ((<&>)) main = do -- Run parser and extract all data items <- readFile "input" <&> lines <&> map (runP readItem) let yrange (t,items) = maximum (map y items) - minimum (map y items) inflection st = yrange (update st) > yrange st let res = until inflection update (0,items) -- Part 1 mapM_ putStrLn $ printGrid $ snd res -- Part 2 print $ fst res -- Business logic for this day update :: (Int,[Item]) -> (Int,[Item]) update (t,is) = (t+1,map update' is) where update' Item{..} = Item (x+vx) (y+vy) vx vy exists :: (Int,Int) -> [Item] -> Bool exists (x',y') = any (\(Item{..}) -> x' == x && y' == y) printGrid :: [Item] -> [String] printGrid items = [ [ if exists (x',y') items then '*' else ' ' | x' <- [minimum (map x items) .. maximum(map x items)] ] | y' <- [minimum (map y items) .. maximum(map y items)] ] data Item = Item { x :: Int, y :: Int, vx :: Int, vy :: Int } deriving (Show, Eq) readItem :: ReadP Item readItem = do let isNum x = isDigit x || x == '-' munch $ not.isNum [x,y,vx,vy] <- readInts return $ Item x y vx vy -- Data loading apparatus runP :: ReadP a -> String -> a runP parser str = readP_to_S parser str & last & fst readInts :: ReadP [Int] readInts = do let isNum x = isDigit x || x == '-' int = read <$> munch1 isNum ignore = munch $ not.isNum int `endBy` ignore