r/adventofcode • • Dec 03 '16

SOLUTION MEGATHREAD --- 2016 Day 3 Solutions ---

--- Day 3: Squares With Three Sides ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


DECKING THE HALLS WITH BOUGHS OF HOLLY 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!

17 Upvotes

234 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 03 '16

Here a Haskell solution w/ parsing:

import Data.Maybe (fromJust)
import Data.List.Split (chunksOf)
import Text.Megaparsec (parseMaybe, space)
import Text.Megaparsec.Lexer (integer)
import Text.Megaparsec.String (Parser)

type Triangle = (Integer, Integer, Integer)

parseTriangles :: String -> [Triangle]
parseTriangles = map (fromJust . parseMaybe parseNums) . lines
    where parseNums :: Parser Triangle
          parseNums = do
            n1 <- space *> integer
            n2 <- space *> integer
            n3 <- space *> integer
            return (n1, n2, n3)

numValidTriangles :: [Triangle] -> Int
numValidTriangles = length . filter isValidTriangle
    where isValidTriangle (a, b, c) = a + b > c && a + c > b && b + c > a

part1 :: String -> String
part1 = show . numValidTriangles . parseTriangles

byCols :: [Triangle] -> [Triangle]
byCols ((a, b, c):(d, e, f):(g, h, i):rest) = (a, d, g) : (b, e, h) : (c, f, i) : byCols rest
byCols [] = []

part2 :: String -> String
part2 = show . numValidTriangles . byCols . parseTriangles