Complete Day 2
authorJack Kinsey <j.jameskinsey@gmail.com>
Sun, 2 Dec 2018 08:27:46 +0000 (03:27 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Sun, 2 Dec 2018 08:27:46 +0000 (03:27 -0500)
day02/boxes.hs [new file with mode: 0644]

diff --git a/day02/boxes.hs b/day02/boxes.hs
new file mode 100644 (file)
index 0000000..b7d322c
--- /dev/null
@@ -0,0 +1,35 @@
+import qualified Data.Map as M
+
+main :: IO ()
+main = do
+    input <- getContents
+    let ids = lines input
+    let count_twos = countN 2 ids
+    let count_threes = countN 3 ids
+    print (count_twos * count_threes)
+    putStrLn . abc $ ids
+
+countN :: Int -> [String] -> Int
+countN check_count = sum . map (go check_count)
+    where go :: Int -> String -> Int
+          go cc test = if any (== cc) . M.elems . countChar $ test
+                       then 1
+                       else 0
+          
+countChar :: String -> M.Map Char Int
+countChar = foldl go M.empty
+    where go :: M.Map Char Int -> Char -> M.Map Char Int
+          go chmap char = M.insert char ((M.findWithDefault 0 char chmap) + 1) chmap
+
+abc :: [String] -> String
+abc (i:is) 
+    | null test = abc is
+    | otherwise = head test
+    where cor_len = (length i) - 1
+          test = filter (\s -> (length s) == cor_len) . map (diff i) $ is
+
+diff :: Eq a => [a] -> [a] -> [a]
+diff [] [] = []
+diff (a:as) (b:bs) 
+    | a == b = (a : diff as bs)
+    | otherwise = diff as bs