-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day8.hs
26 lines (22 loc) · 819 Bytes
/
Day8.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
{-|
Module: Day8
Description: <https://adventofcode.com/2018/day/8 Day 8: Memory Maneuver>
-}
{-# LANGUAGE FlexibleContexts, LambdaCase, TypeApplications #-}
module Day8 (day8a, day8b) where
import Data.Function (on)
import Text.Megaparsec (MonadParsec, count, parseMaybe)
import Text.Megaparsec.Char (space)
import Text.Megaparsec.Char.Lexer (decimal)
parser :: (MonadParsec e String m, Integral i) => ([i] -> [i] -> i) -> m i
parser f = parser'
where parser' = do
n <- decimal <* space
m <- decimal <* space
f <$> count n parser' <*> count m (decimal <* space)
day8a :: String -> Maybe Int
day8a = parseMaybe $ parser @() $ on (+) sum
day8b :: String -> Maybe Int
day8b = parseMaybe $ parser @() $ \case
[] -> sum
xs -> sum . map ((0 : xs ++ repeat 0) !!)