summaryrefslogtreecommitdiff
path: root/2015/day19.hs
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-14 20:19:02 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-14 20:19:02 +0100
commit2d02f553aa4cc4ded630628eccdf34f55937cee5 (patch)
treed5377ebdff68788725b5820d5331ce7b6c9d4a84 /2015/day19.hs
parent97b4c5d86cc12447ac6845e25a863e26a88aec35 (diff)
Add 2015 sources
Diffstat (limited to '2015/day19.hs')
-rw-r--r--2015/day19.hs41
1 files changed, 41 insertions, 0 deletions
diff --git a/2015/day19.hs b/2015/day19.hs
new file mode 100644
index 0000000..65d3e87
--- /dev/null
+++ b/2015/day19.hs
@@ -0,0 +1,41 @@
+import Data.List
+import qualified Data.Set as Set
+import Control.Monad
+
+import Debug.Trace
+
+parseRepl :: [String] -> (String,String)
+parseRepl [a,"=>",b] = (a,b)
+-- = -- subl syntax
+
+startsWith :: String -> String -> Bool
+startsWith s prefix = take (length prefix) s == prefix
+
+getRepls :: [(String,String)] -> String -> Set.Set String
+getRepls rs s = getRepls' rs s ""
+
+getRepls' :: [(String,String)] -> String -> String -> Set.Set String
+getRepls' _ [] _ = Set.empty
+getRepls' rs s prefix = foldl (\set s -> Set.insert (prefix++s) set) fromnext
+ $ map (\r -> replace s r)
+ $ filter (\r -> s `startsWith` (fst r)) rs
+ where fromnext = getRepls' rs (tail s) $ prefix ++ [head s]
+ replace s r = snd r ++ drop (length (fst r)) s
+
+day19_1 :: IO ()
+day19_1 = do
+ input <- liftM lines $ readFile "day19.txt"
+ let rs = map (parseRepl . words) $ init (init input)
+ start = last input
+ print $ Set.size $ getRepls rs start
+
+day19_2 :: IO ()
+day19_2 = do
+ input <- liftM lines $ readFile "day19.txt"
+ let rs = map (parseRepl . words) $ init (init input)
+ target = last input
+ stages = iterate (Set.unions . map (getRepls rs) . Set.toList . (\s -> trace (show $ Set.size s) s)) $ Set.fromList ["e"]
+ print $ (Set.unions . map (getRepls rs) . Set.toList) $ (Set.unions . map (getRepls rs) . Set.toList) $ Set.fromList ["e"]
+ print $ findIndex (Set.member target) stages
+
+main = day19_2