-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
34 lines (26 loc) · 884 Bytes
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{-# LANGUAGE RecordWildCards #-}
import Data.Maybe
import Text.Parsec
data Box = Box { w :: Integer, h :: Integer, l :: Integer}
deriving Show
measure = read <$> many1 digit
eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right x) = Just x
readBox :: String -> Maybe Box
readBox = eitherToMaybe . parse box ""
where box = Box <$> measure
<*> (char 'x' *> measure)
<*> (char 'x' *> measure)
paper :: Box -> Integer
paper Box{..} = sum (map (2*) sides) + minimum sides
where sides = [w*l, l*h, w*h]
ribbon :: Box -> Integer
ribbon Box{..} = minimum sides + (w*l*h)
where sides = map (*2) [w+l, l+h, w+h]
part1 = sum . map paper . mapMaybe readBox . lines
part2 = sum . map ribbon . mapMaybe readBox . lines
main = do
input <- readFile "input.txt"
print (part1 input)
print (part2 input)