DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 5: Binary Boarding

Collapse
 
justinh11235 profile image
Justin Hinckley

My Haskell Solution (I just started learning Haskell so fair warning):

import Data.Bool (bool)

toDecimalList :: [String] -> [Int]
toDecimalList = map (foldl1 ((+) . (2*)) . map (bool 0 1 . (`elem` "BR")))

ans1 :: [String] -> Int
ans1 = maximum . toDecimalList

findMissing :: [Int] -> Int
findMissing nums = (minimum nums + maximum nums) * (length nums + 1) `div` 2 - sum nums

ans2 :: [String] -> Int
ans2 = findMissing . toDecimalList

main :: IO ()
main = do
    contents <- readFile "adventofcode5input"
    print $ ans1 $ lines contents -- Part 1
    print $ ans2 $ lines contents -- Part 2
Enter fullscreen mode Exit fullscreen mode