-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.hs
43 lines (36 loc) · 1.09 KB
/
Common.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
35
36
37
38
39
40
41
42
43
{-# LANGUAGE OverloadedStrings #-}
module Common where
import Control.Exception
import qualified Data.Map as Map
import Data.Map (Map)
import Formatting
import Formatting.Clock
import Linear.V2
import System.Clock
import Text.Printf
data Solution a b c =
Solution
{ parse :: String -> a
, part1 :: a -> b
, part2 :: a -> c
}
benchmark :: IO a -> IO ()
benchmark action = do
start <- getTime Monotonic
action
end <- getTime Monotonic
fprint (" (" % timeSpecs % ")\n") start end
aoc :: (Show b, Show c) => Int -> Solution a b c -> IO ()
aoc n solution = do
input <- readFile $ (printf "%02d" n) ++ ".txt"
let inputWithoutNewline = init input
let problem = parse solution $ inputWithoutNewline
benchmark $ do
putStr "Parsing input..."
evaluate problem
benchmark $ putStr $ "Part 1: " ++ show (part1 solution $ problem)
benchmark $ putStr $ "Part 2: " ++ show (part2 solution $ problem)
tbd :: a -> String
tbd x = "(not implemented)"
freqs :: Ord a => [a] -> Map a Integer
freqs = Map.fromListWith (+) . map ((flip (,)) 1)