Skip to content

Commit 0a22418

Browse files
committed
refactor(2020.18-haskell): adopt advent-of-code-api
1 parent 7521854 commit 0a22418

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2024.7.2.83
1+
2024.7.2.84

src/AdventOfCode/Year2020/Day18.hs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
13
module AdventOfCode.Year2020.Day18
24
( main,
35
partOne,
46
partTwo,
57
)
68
where
79

8-
import AdventOfCode.Input (parseInput)
9-
import AdventOfCode.TH (inputFilePath)
10-
import Control.Applicative ((<|>))
11-
import Control.Monad.Combinators.Expr
12-
import Text.Trifecta (Parser, natural, parens, some, symbol)
10+
import AdventOfCode.Input (parseString, rawInputAoC)
11+
import AdventOfCode.Puzzle
12+
import AdventOfCode.TH (defaultMainPuzzle)
13+
import Control.Monad.Combinators.Expr (Operator (InfixL), makeExprParser)
14+
import Relude
15+
import Text.Trifecta (Parser, natural, parens, symbol)
1316

1417
main :: IO ()
15-
main =
16-
do
17-
putStr "Part One: "
18-
print =<< partOne
19-
putStr "Part Two: "
20-
print =<< partTwo
21-
22-
partOne :: IO Int
23-
partOne = sum <$> parseInput (some expr) $(inputFilePath)
24-
where
25-
expr = makeExprParser (parens expr <|> posInt) [[addition, multiplication]]
18+
main = $(defaultMainPuzzle)
19+
20+
partOne :: SimplePuzzle String Integer
21+
partOne = solve [[multiplication, addition]]
22+
23+
partTwo :: SimplePuzzle String Integer
24+
partTwo = solve [[addition], [multiplication]]
25+
26+
getInput :: IO String
27+
getInput = rawInputAoC 2020 18
2628

27-
partTwo :: IO Int
28-
partTwo = sum <$> parseInput (some expr) $(inputFilePath)
29+
solve :: [[Operator Parser Integer]] -> SimplePuzzle String Integer
30+
solve table = ask >>= parseString (some expr) <&> sum
2931
where
30-
expr = makeExprParser (parens expr <|> posInt) [[addition], [multiplication]]
32+
expr = makeExprParser (parens expr <|> natural) table
3133

32-
addition :: Operator Parser Int
33-
addition = InfixL ((+) <$ symbol "+")
34+
addition :: (Num a) => Operator Parser a
35+
addition = binary "+" (+)
3436

35-
multiplication :: Operator Parser Int
36-
multiplication = InfixL ((*) <$ symbol "*")
37+
multiplication :: (Num a) => Operator Parser a
38+
multiplication = binary "*" (*)
3739

38-
posInt :: Parser Int
39-
posInt = fromInteger <$> natural
40+
binary :: String -> (a -> a -> a) -> Operator Parser a
41+
binary name f = InfixL (f <$ symbol name)

0 commit comments

Comments
 (0)