From ab438d84d2892aa5e0be67f3f15029f5ce39ac07 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sat, 1 Dec 2018 14:52:17 +0100 Subject: [PATCH] day1: add solution --- .gitignore | 1 + Day1A.hs | 8 ++++++++ Day1B.hs | 14 ++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 .gitignore create mode 100644 Day1A.hs create mode 100644 Day1B.hs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14140f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.in diff --git a/Day1A.hs b/Day1A.hs new file mode 100644 index 0000000..7a05045 --- /dev/null +++ b/Day1A.hs @@ -0,0 +1,8 @@ +module Day1A where + +read' :: String -> Integer +read' ('+':str) = read str +read' str = read str + +main :: IO () +main = interact $ show . sum . map read' . lines diff --git a/Day1B.hs b/Day1B.hs new file mode 100644 index 0000000..e9553fb --- /dev/null +++ b/Day1B.hs @@ -0,0 +1,14 @@ +module Day1B where + +import Day1A (read') +import qualified Data.Set as S + +handle :: Integer -> (S.Set Integer) -> [Integer] -> Integer +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 Integers and repeat infinitely