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!

16 Upvotes

234 comments sorted by

View all comments

Show parent comment

2

u/CremboC Dec 03 '16

I have basically the same as yours:

import Data.List.Split
import Data.List
import Data.Char

-- convert "  142  23 543" -> [142, 23, 543]
-- words splits into words (delimited by whitespace)
-- map read converts the strings: ["142", "23", "543"] -> [142, 23, 543]
parse :: String -> [Int]
parse tri = map read $ words tri

-- checks there the given triangle [a, b, c] is a valid
-- valid triangle: sum of two edges is more than the third
triangle' :: [Int] -> Bool
triangle' [a, b, c] = a < b + c && b < a + c && c < b + a

main :: IO ()
main = do
    input <- readFile "input.txt"
    let parsed = map parse (lines input)

    -- part 1
    print $ length $ filter triangle' parsed

    -- part 2
    -- 1. split the original parsed input into groups of 3 [[[x, y, z], [a, b, c], [f, g, h]], ..]
    -- 2. take each bunch and transpose it [[x, y, z], [a, b, c], [f, g, h]] --> [[x, a, f], [y, b, g], [z, c, h]]
    -- 3. concat == flatten all the bunches
    -- 4-5. get length of valid triangles
    print $ length $ filter triangle' $ concat $ map transpose $ chunksOf 3 parsed

with comments since I'm a nice person.