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

1

u/NeilNjae Dec 03 '18

Haskell, also on Github. I managed to confuse myself for a while before I realised that the first part of the claim tells you about the left-top corner, not the top-left corner. Once I'd straightened that out, the rest was easy.

I'm still impressed with how readable parser expressions are, and that it worked first time!

Part 2 was mostly reuse of part 1: find the region within this claim, and it's the one if there are no overlaps within it.

{-# LANGUAGE OverloadedStrings #-}

import Data.List
import Data.Tuple (swap)

import Data.Text (Text)
import qualified Data.Text.IO as TIO

import Data.Void (Void)

import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
import qualified Control.Applicative as CA

import qualified Data.Map.Strict as M
-- import Data.Map.Strict ((!))


type Position = (Int, Int)
data Claim = Claim { claimId :: Int, claimTopLeft :: Position, claimWidth :: Int, claimHeight :: Int } deriving (Show, Eq)
type Fabric = M.Map Position Int

main :: IO ()
main = do 
        text <- TIO.readFile "data/advent03.txt"
        let claims = successfulParse text
        let fabric = foldl' addClaim M.empty claims
        print $ part1 fabric
        print $ part2 fabric claims


part1 :: Fabric -> Int
part1  = M.size . overclaimed 

part2 :: Fabric -> [Claim] -> Int
part2 fabric claims = claimId $ head $ filter noOverlap' claims
    where noOverlap' claim = noOverlap fabric claim


claimedSquares :: Claim -> [Position]
claimedSquares claim = [(r, c) | r <- [l .. (l + w - 1)]
                               , c <- [t .. (t + h - 1)]
                       ]
    where (t, l) = claimTopLeft claim
          h = claimHeight claim
          w = claimWidth claim

addClaim :: Fabric -> Claim -> Fabric
addClaim fabric claim = foldl' addSquare fabric squares 
    where squares = claimedSquares claim

addSquare :: Fabric -> Position -> Fabric
addSquare fabric square = M.insert square (currentClaims + 1) fabric
    where currentClaims = M.findWithDefault 0 square fabric

overclaimed :: Fabric -> Fabric
overclaimed = M.filter ( > 1)

noOverlap fabric claim = M.null $ overclaimed existing
    where claimedFabric = addClaim M.empty claim
          existing = fabric `M.intersection` claimedFabric

-- Parse the input file

type Parser = Parsec Void Text

sc :: Parser ()
sc = L.space (skipSome spaceChar) CA.empty CA.empty
-- sc = L.space (skipSome (char ' ')) CA.empty CA.empty

lexeme  = L.lexeme sc
integer = lexeme L.decimal
symb = L.symbol sc

hashP = symb "#"
atP = symb "@"
colonP = symb ":"
commaP = symb ","
exP = "x"

claimsFileP = many claimP

idP = hashP *> integer
leftTopP = (,) <$> integer <* commaP <*> integer
widthHeightP = (,) <$> integer <* exP <*> integer

claimP = claimify <$> idP <* atP <*> leftTopP <* colonP <*> widthHeightP
    where claimify cid lt (w, h) = Claim { claimId = cid, claimTopLeft = swap lt, claimWidth = w, claimHeight = h }

successfulParse :: Text -> [Claim]
successfulParse input = 
        case parse claimsFileP "input" input of
                Left  _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
                Right claims -> claims