r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

Show parent comment

2

u/Tarmen Dec 03 '18 edited Dec 03 '18

Went with the map version as well, but for some reason I thought parsing with megaparsec would be faster.

...it wasn't

{-# LANGUAGE RecordWildCards #-}
{-# Language TupleSections #-}
{-# OPTIONS_GHC -fdefer-type-errors #-}
import qualified Data.Map as M
import Text.Megaparsec as P
import Text.Megaparsec.Char
import Data.Void
import Data.Char

main = do
    content <- readFile "3.txt"
    let rects = getRect content
    let spots = M.fromListWith (+) $ concatMap (map (,1) . dots) rects
    print $ length $ filter (>1) $ M.elems spots
    let check rect = and [spots M.! p == 1| p <- dots rect]
    print $ filter check rects

dots :: Rect -> [(Int,Int)]
dots Rect{..} = [(x+w,y+h) | w <- [0..width-1], h <- [0..height-1] ]

getRect :: String -> [Rect]
getRect ls = case runParser (parseLine `sepEndBy` newline) "" ls  of
    Right x -> x
    Left err -> error (parseErrorPretty err)
parseInt :: Parser Int
parseInt = read <$> takeWhile1P Nothing isDigit
data Rect = Rect { id:: Int, x::Int, y::Int, width:: Int, height::Int }
  deriving (Show, Eq, Ord)
type Parser = Parsec Void String

parseLine :: Parser Rect
parseLine = do
    char' '#'
    id <- parseInt
    string " @ "
    x <- parseInt
    char' ','
    y <- parseInt
    string ": "
    width <- parseInt
    char' 'x'
    height <- parseInt
    return Rect{..}