-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.hs
More file actions
57 lines (47 loc) · 1.47 KB
/
run.hs
File metadata and controls
57 lines (47 loc) · 1.47 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
import Data.List.Split (splitOn)
import Data.Maybe (mapMaybe)
import Data.String (fromString)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as HashMap
type S = ByteString
type Input = ([S], [S])
parseTargets :: String -> [S]
parseTargets = map fromString . lines
parseTowels :: String -> [S]
parseTowels =
map fromString
. filter (not . null)
. map (filter (\c -> 'a' <= c && c <= 'z'))
. splitOn ","
parseAll :: String -> Input
parseAll input =
let [towels, targets] = splitOn "\n\n" input
in (parseTowels towels, parseTargets targets)
search :: [S] -> S -> Int
search towels t = go t
where memoized = HashMap.fromList $ map (\x -> (x, go x)) $ BS.tails t
go = \case x | BS.null x -> 1
| otherwise -> sum $ map (memoized HashMap.!) (matching x)
matching x = mapMaybe (`BS.stripPrefix` x) towels
parts :: Input -> [Int]
parts (towels, targets) =
let counts = map (search towels) targets
in [ length (filter (> 0) counts)
, sum counts
]
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
mapM_ print (parts input)