From 8f030af74c4b39732d73abfb60d62121b27bedc9 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 2 Dec 2018 16:52:36 +0100 Subject: [PATCH] Add day2 and readd day1 --- .gitignore | 2 ++ Day1/Day1A.hs | 8 ++++++++ Day1/Day1B.hs | 14 ++++++++++++++ Day2/Day2A.hs | 18 ++++++++++++++++++ Day2/Day2B.hs | 25 +++++++++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 Day1/Day1A.hs create mode 100644 Day1/Day1B.hs create mode 100644 Day2/Day2A.hs create mode 100644 Day2/Day2B.hs diff --git a/.gitignore b/.gitignore index 65dd287..cd4e12a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ # Ignore everything without extension * +!*.* !*/ *.in *.hi *.o +*.prof diff --git a/Day1/Day1A.hs b/Day1/Day1A.hs new file mode 100644 index 0000000..4ed7b42 --- /dev/null +++ b/Day1/Day1A.hs @@ -0,0 +1,8 @@ +module Day1A where + +read' :: String -> Int +read' ('+':str) = read str +read' str = read str + +main :: IO () +main = interact $ show . sum . map read' . lines diff --git a/Day1/Day1B.hs b/Day1/Day1B.hs new file mode 100644 index 0000000..04db47a --- /dev/null +++ b/Day1/Day1B.hs @@ -0,0 +1,14 @@ +module Day1B where + +import Day1A (read') +import qualified Data.IntSet as S + +handle :: Int -> S.IntSet -> [Int] -> Int +handle freq history (nextDelta:xs) + | S.member freq history = freq -- When the frequency is in the history set, return it + | otherwise = handle (freq + nextDelta) (S.insert freq history) xs -- Recurse otherwise +handle _ _ _ = error "xs should not be empty :(" + +main :: IO () +main = interact $ show . handle 0 S.empty . deltaFreqs + where deltaFreqs = cycle . map read' . lines -- Convert input to Ints and repeat infinitely diff --git a/Day2/Day2A.hs b/Day2/Day2A.hs new file mode 100644 index 0000000..b6526a4 --- /dev/null +++ b/Day2/Day2A.hs @@ -0,0 +1,18 @@ +module Day2A where + +import qualified Data.IntMap as IM +import Data.Char (ord) + +out :: String -> Int -> Int +out id' n = min (length $ IM.filter (==n) counts) 1 + where counts = foldr (\c -> IM.insertWith (+) (ord c) 1) IM.empty id' + +handleId :: String -> (Int, Int) +handleId id' = (out id' 2, out id' 3) + +add2 :: Num a => (a, a) -> (a, a) -> (a, a) +add2 (a, b) (x, y) = (a + x, b + y) + +main :: IO () +main = interact $ show . uncurry (*) . counts . lines + where counts = foldr (add2 . handleId) (0, 0) diff --git a/Day2/Day2B.hs b/Day2/Day2B.hs new file mode 100644 index 0000000..0ba6b24 --- /dev/null +++ b/Day2/Day2B.hs @@ -0,0 +1,25 @@ +{-# LANGUAGE TupleSections #-} +module Day2B where +import Data.List (find) +import qualified Data.Sequence as S +import qualified Data.Foldable as F + +dist' :: Int -> (S.Seq Char) -> String -> String -> (Int, String) +dist' n common (a:x) (b:y) + | a == b = dist' n (common S.|> a) x y + | otherwise = dist' (n + 1) common x y +dist' n common _ _ = (n, F.toList common) + +dist :: String -> String -> (Int, String) +dist = dist' 0 S.empty + +combinations :: [String] -> [(String, String)] +combinations (x:y:xs) = (x, y):(combinations (y:xs)) +combinations [] = [] + +distances :: [(String, String)] -> String +distances xs = result + where Just(_, result) = find (\(x, _) -> x == 1) $ map (uncurry dist) xs + +main :: IO () +main = interact $ distances . combinations . lines