-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay19.hs
32 lines (25 loc) · 942 Bytes
/
Day19.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
module Day19 where
import Data.List
import Debug.Trace
import Data.Array
parse :: String -> ([String],[String])
parse s = (towels,problems)
where stowels:"":problems = lines s
towels = words $ filter (/=',') stowels
slurp = parse <$> readFile "input-19"
possible towels prob = arr!0
where n = length prob
arr = array (0,n) $ [(i,p i) | i <- [0..n]]
p i
| i==n = 1
| otherwise = let s = drop i prob
in case [i + length t | t <- towels, t `isPrefixOf` s]
of [] -> 0
is -> sum $ map (arr!) is
exampleTowels = ["r", "wr", "b", "g", "bwu", "rb", "gb", "br"]
exampleProblems = ["brwrr", "bggr", "gbbr", "rrbgbr", "ubwu", "bwurrg", "brgr", "bbrgwb"]
part1 (ts,ps) = length (filter ((0<) . possible ts) ps)
part2 (ts,ps) = sum $ map (possible ts) ps
main = do
--print . part1 =<< slurp
print . part2 =<< slurp